Thursday, September 1, 2016

Java interview questions for experienced - set 1

What is the major advantage of autoboxing?
   Autoboxing is the automatic conversion that the Java compiler makes between the primitive types     and their corresponding object wrapper classes. For example, converting an int to an Integer, a     double to a Double, and so on. If the conversion goes the other way, this is called unboxing.
 
   For Ex : Arraylist.add(5);  // here primitive type converted to object using autoBoxing
 
   Advantage : lesser code , looks clear
   DisAdvantage : Can lead to unexpected behaviour :  esp in comparission "equal / == " .. equal meant for object whereas == meant for primitive type
             NPE : You can get a NullPointerException, if the wrapper object is null and is unboxed. Pointing out the obvious there can’t be a NullPointer  with primitive variables, but they can have the value zero.


Difference between IsInstance and Instanceof.
   Instanceof : when you know the type in compile time   (  obj instanceof MyCustomActivity)
   IsInstance  : type at runtime  ( Class.forName(arg[0]).Isinstance(obj) )

Reason Why Wait , Notify and NotifyAll are in Object Class.
  a) Wait and notify is not just normal methods or synchronization utility, more than that they are communication mechanism between two threads in Java
  b) Locks are made available on per Object basis, which is another reason wait and notify is declared in Object class



Synchronized and Concurrenthashmap

SynchronizedHashMap
       1.Synchronization at Object level.
       2. Every read/write operation needs to acquire lock.
       3. Locking the entire collection is a performance overhead.
          This essentially gives access to only one thread to the entire map & blocks all the other threads.
    ConcurrentHashMap
       1. You should use ConcurrentHashMap when you need very high concurrency in your project.
       2. It is thread safe without synchronizing the whole map.
       3. Reads can happen very fast while write is done with a lock.
       4. There is no locking at the object level.
    The locking is at a much finer granularity at a hashmap bucket level.


to know hashmap internal


String vs StringBuffer vs StringBuilder










No comments:

Post a Comment