Sunday 7 August 2016

Core Java Concepts

Q. What is Serialization  in java ?
Ans .Serialization is simply turning an existing object into a byte array. This byte array represents the class of the object, the version of the object, and the internal state of the object. This byte array can then be used between JVM's running the same code to transmit/read the object.
Why would we want to do this?
There are several reasons:
  • Communication: If you have two machines that are running the same code, and they need to communicate, an easy way is for one machine to build an object with information that it would like to transmit, and then serialize that object to the other machine. It's not the best method for communication, but it gets the job done.
  • Persistence: If you want to store the state of a particular operation in a database, it can be easily serialized to a byte array, and stored in the database for later retrieval.
  • Deep Copy: If you need an exact replica of an Object, and don't want to go to the trouble of writing your own specialized clone() class, simply serializing the object to a byte array, and then de-serializing it to another object achieves this goal.
  • Caching: Really just an application of the above, but sometimes an object takes 10 minutes to build, but would only take 10 seconds to de-serialize. So, rather than hold onto the giant object in memory, just cache it out to a file via serialization, and read it in later when it's needed.
  • Cross JVM Synchronization: Serialization works across different JVMs that may be running on different architectures.

  • Example : While you're running your application, all of its objects are stored in memory (RAM). When you exit, that memory gets reclaimed by the operating system, and your program essentially 'forgets' everything that happened while it was running. Serialization remedies this by letting your application save objects to disk so it can read them back the next time it starts. If your application is going to provide any way of saving/sharing a previous state, you'll need some form of serialization.
Q.What is JVM architceture in java ?

Ans .
The JVM performs following operation:
  • Loads code
  • Verifies code
  • Executes code
  • Provides runtime environment
JVM provides definitions for the:
  • Memory area
  • Class file format
  • Register set
  • Garbage-collected heap
  • Fatal error reporting etc.

Internal Architecture of JVM 

 

JVM has various sub components internally. You can see all of them from the above diagram.

1. Class loader sub system: JVM's class loader sub system performs 3 tasks
      a. It loads .class file into memory.
      b. It verifies byte code instructions.
      c. It allots memory required for the program.

2. Run time data area: This is the memory resource used by JVM and it is divided into 5 parts
      a. Method area: Method area stores class code and method code.
      b. Heap: Objects are created on heap.
      c. Java stacks: Java stacks are the places where the Java methods are executed. A Java stack contains frames. On each frame, a separate method is executed.
      d. Program counter registers: The program counter registers store memory address of the instruction to be executed by the micro processor.
      e. Native method stacks: The native method stacks are places where native methods (for example, C language programs) are executed. Native method is a function, which is written in another language other than Java.

3. Native method interface: Native method interface is a program that connects native methods libraries (C header files) with JVM for executing native methods.

4. Native method library: holds the native libraries information.

5. Execution engine: Execution engine contains interpreter and JIT compiler, which covert byte code into machine code. JVM uses optimization technique to decide which part to be interpreted and which part to be used with JIT compiler. The HotSpot represent the block of code executed by JIT compiler.

Q .Collections in java :

Ans .

Lists 


A list is an ordered collection of elements that allows duplicate entries. Lists
implement the List interface, and elements in a list can be accessed by an integer index.



The different list classes each provide their own unique functionality:

ArrayList A resizable list implemented as an array. When elements are added and
removed, the ArrayList grows and shrinks accordingly. You can control the internal size of
the array to improve performance.

LinkedList A list that implements a linked list data structure. Items can be added and
removed from the beginning or end of the linked list. LinkedList is unique in that it also
implements the Queue and Deque interfaces.

Vector A Vector is essentially the same as an ArrayList except that the methods in
Vector are synchronized.

Stack A list that implements a stack data. Items are pushed onto the top and popped off
the top of the stack, a last - in, first - out (LIFO) behavior.

Note.The basic operations of List include the ability to add a single element at a specifi ed
index, add a collection of elements, replace or remove a specifi c element, and retrieve an

element at a specified index.

Sets


A set is a collection that does not allow duplicate entries. Sets implement the Set
interface.



The different set classes each provide their own unique functionality:

HashSet A set that stores its elements in a hash table. There is no ordering to the items,
and HashSet uses the hashCode method of its elements to determine their placing in the set.

LinkedHashSet A set that stores its elements in a linked list hash table. The items are
hashed based on their hashCode and also ordered in a doubly linked list.

TreeSet A set that stores its elements in a tree data structure that is also sorted and navigable.

The add , remove , and contains methods are guaranteed to work in log( n ) time,

where n is the number of elements in the tree.

Queues 


A queue is a collection whose elements are added and removed in a specific order. Queues
are typically used for storing elements prior to processing them. For example, suppose you
have an order processing application that places new orders into a queue. The warehouse
could retrieve the order from the queue to fulfi ll the order, and the billing department could
retrieve the order from the queue to collect payment. Queues typically process elements in a

FIFO behavior, but the actual behavior depends on the type of queue you are using.

The Queue and Deque interfaces and implementing classes :


The different queue and deque classes each have their own specific behaviors:

PriorityQueue A queue where the elements are ordered based on an ordering you specify
(as opposed to ordering based on FIFO).

LinkedList The same LinkedList class we saw earlier in the discussion on lists.
LinkedList also implements the Queue and Deque interfaces, providing a queue or deque
that is implemented as a linked list data structure.

ArrayDeque A queue and deque implemented as a resizable array with no capacity
restrictions.

The basic operations of Queue include adding a single element, polling the queue to
retrieve the next element, or peeking at the queue to see if there is an element available
in the queue. The Deque operations are similar except elements can be added, polled, or

peeked at both the beginning and end of the deque.

Maps 


A map is a collection that maps keys to values, with no duplicate keys allowed.
The elements in a map are key - value pairs. Maps implement the Map interface, which is
unique because the Map interface is not a subinterface of Collection like the other types of

collections.

The Map interfaces and implementing classes :




HashMap A map that stores its elements in a hash table. There is no ordering to the
elements, and they are placed in the hash table based on their hashCode .

LinkedHashMap A map that stores its element in a hash table and doubly linked lists. The
linked list provides an ordering to the elements.

TreeMap A map that stores its elements in a tree data structure with a natural or
user - defined ordering. TreeMap provides log( n ) time for the methods that view or change
elements in the tree.

HashMap vs ConcurrentHashMap :

1.  Thread -Safe : ConcurrentHashMap is thread-safe that is the code can be accessed by single thread at a time .  while HashMap is not thread-safe .

2.  Synchronization Method : HashMap can be synchronized by using    
synchronizedMap(HashMap)  method . By using this method we get a HashMap object which is equivalent to the HashTable object . So every modification  is performed on  Map is locked on Map object.
ConcurrentHashMap synchronizes or locks on the certain portion of the Map . To optimize
the performance of ConcurrentHashMap , Map is divided into different partitions depending upon the Concurrency level . So that we do not need to synchronize the whole Map Object.

3. Performance  : In multiple threaded environment HashMap is usually faster than ConcurrentHashMap . As only single thread can access the certain portion of the Map and thus reducing the performance . While in HashMap any number of threads can access the code at the same time .


The Map interface provides methods for accessing the elements of the collection as a set
of keys, a list of values, or a set of key - value mappings. We discuss maps in detail in the
section “ Using Generics ” later in this chapter as well as the details for using lists, sets, and
queues.  

Q. Which collection classes are synchronized or thread-safe ?

Ans .
Stack, Properties , Vector and Hashtable can be used in multi threaded environment because they are synchronized classes (or thread-safe). 

No comments:

Post a Comment