Tuesday, July 21, 2015

Differences between Java 5, 6,7 and 8


Java 5

  • Generics.
  • Annotations.
  • AutoBoxing
  • Varargs
  • for each
  • Static Imports
  • java.util.concurrent
Java 6
  • Scripting Language Support (Rhino)
  • JAX-WS
  • Java Compiler API
Java 7
  • Project Coin
    • Strings in switch
    • Automatic Resource Management
    • Diamond Operator
  • Java NIO file API
  • Timsort is used to sort collections and arrays of objects instead of merge sort.
  • Elliptic curve cryptography
Java 8
  • lambda expressions
  • default methods
  • Annotation on Java Types
  • Repeating annotations
  • Date and Time API
  • Remove the permanent generation

Friday, September 14, 2012

JVM Questions and description



1)   What is Phantom reference and where is it use?

A)   Phantom reference objects, which are enqueued after the collector determines that their referents may otherwise be reclaimed. In other words when a phantom reference is finalized the reference is queued in the ReferenceQueue. This can be used in implementing connection pools, profiling and to check if a particular object is garbage collected.

2)      What is WeakReference and where is it used?

A)    Weak reference objects, which do not prevent their referents from being made finalizable, finalized, and then reclaimed. Weak references are most often used to implement canonicalizing mappings.
The difference is in exactly when the enqueuing happens. WeakReferences are enqueued as soon as the object to which they point becomes weakly reachable. This is before finalization or garbage collection has actually happened; in theory the object could even be "resurrected" by an unorthodox finalize() method, but the WeakReference would remain dead. PhantomReferences are enqueued only when the object is physically removed from memory, and the get() method always returns null specifically to prevent you from being able to "resurrect" an almost-dead object.
What good are PhantomReferences? I'm only aware of two serious cases for them: first, they allow you to determine exactly when an object was removed from memory. They are in fact the only way to determine that. This isn't generally that useful, but might come in handy in certain very specific circumstances like manipulating large images: if you know for sure that an image should be garbage collected, you can wait until it actually is before attempting to load the next image, and therefore make the dreaded OutOfMemoryError less likely.
Second, PhantomReferences avoid a fundamental problem with finalization: finalize() methods can "resurrect" objects by creating new strong references to them. So what, you say? Well, the problem is that an object which overrides finalize() must now be determined to be garbage in at least two separate garbage collection cycles in order to be collected. When the first cycle determines that it is garbage, it becomes eligible for finalization. Because of the (slim, but unfortunately real) possibility that the object was "resurrected" during finalization, the garbage collector has to run again before the object can actually be removed. And because finalization might not have happened in a timely fashion, an arbitrary number of garbage collection cycles might have happened while the object was waiting for finalization. This can mean serious delays in actually cleaning up garbage objects, and is why you can get OutOfMemoryErrors even when most of the heap is garbage.
The garbage collector enqueues soft, weak, and phantom reference objects in different situations to indicate three different kinds of reachability state changes. The meanings of the six reachability states and the circumstances under which state changes occur are as follow:
  • Strongly reachable - An object can be reached from the roots without traversing any reference objects. An object begins its lifetime in the strongly reachable state and remains strongly reachable so long as it is reachable via a root node or another strongly reachable object. The garbage collector will not attempt to reclaim the memory occupied by a strongly reachable object.
  • Softly reachable - An object is not strongly reachable, but can be reached from the roots via one or more (un-cleared) soft reference objects. The garbage collector may reclaim the memory occupied by a softly reachable object. If it does so, it clears all soft references to that softly reachable object. When the garbage collector clears a soft reference object that is associated with a reference queue, it enqueues that soft reference object.
  • Weakly reachable - An object is neither strongly nor softly reachable, but can be reached from the roots via one or more (un-cleared) weak reference objects. The garbage collector must reclaim the memory occupied by a weakly reachable object. When it does so, it clears all the weak references to that weakly reachable object. When the garbage collector clears a weak reference object that is associated with a reference queue, it enqueues that weak reference object.
  • Resurrectable - An object is neither strongly, softly, or weakly reachable, but may still be resurrected back into one of those states by the execution of some finalizer.
  • Phantom reachable - An object is not strongly, softly, nor weakly reachable, has been determined to not be resurrectable by any finalizer (if it declares a finalize() method itself, then its finalizer will have been run), and is reachable from the roots via one or more (uncleared) phantom reference objects. As soon as an object referenced by a phantom reference object becomes phantom reachable, the garbage collector will enqueue it. The garbage collector will never clear a phantom reference. All phantom references must be explicitly cleared by the program.
  • Unreachable - An object is neither strongly, softly, weakly, nor phantom reachable, and is not resurrectable. Unreachable objects are ready for reclamation. more..
3) What is the difference between WeakReference and PhantomReference?

A) A WeakReference is garbage collected when the next cycle of garbage collection. This kind of references can be used for canonicalizing mappings. PhantomReference is a reference which is pathomly reachable i.e the referent is finalized and ready for garbage collection. The difference is that phantom references are placed in a queue when the referent is garbage collected. The WeakReference is queued when it is weakly referenced before or after marked for garbage collection. A WeakReference can be resurrected but a PhantomReference can never be.


4) What is referent and what is a reference, when will each be garbage collected?

A) A referent is the actual object which is wrapped in a reference. When the referent is no longer used as the reference to the referent is not strongly reachable it will be garbage collected based on the ref. The reference itself needs to be garbage collected as normally it will have a strong ref from the holding object. To garbage collect the same the ReferenceQueue is used to track the references whose referents are no longer used and can be cleared for garbage collection in code.

5) What is a SoftReference and what is its use?
A) Soft reference objects, which are cleared at the discretion of the garbage collector in response to memory demand.  Soft references are most often used to implement memory-sensitive caches.

6) What are the different regions in JVM?

A) There are 4 memory regions in a JVM, they are  the registers, the stack, the garbage-collected heap, and the method area. 

7) What is the size of address word in JVM?

A) The size of an address in the JVM is 32 bits. So it can access a maximum of 4 GB (2 ^ 32)

8) How many registers are there in the JVM?

A) Total 4 registers, they are the program counter, or pc register, optop register, frame register, and vars register(Except pc remaining three point to various parts of the stack frame of the currently executing method).

9) Where does the byte code reside in the JVM?.

A) The method area is where the byte codes reside. The program counter always points to (contains the address of) some byte in the method area. The program counter is used to keep track of the thread of execution. After a bytecode instruction has been executed, the program counter will contain the address of the next instruction to execute. After execution of an instruction, the JVM sets the program counter to the address of the instruction that immediately follows the previous one, unless the previous one specifically demanded a jump.

10) What is a Java Stack in JVM and why is it used?

A) The Java stack is used to store parameters for and results of bytecode instructions, to pass parameters to and return values from methods, and to keep the state of each method invocation. The state of a method invocation is called its stack frame. The vars, frame, and optop registers point to different parts of the current stack frame.

11) What are the 3 important sections of the Java Stack? Explain them?.

A) There are three sections in a Java stack frame: the local variables, the execution environment, and the operand stack. The local variables section contains all the local variables being used by the current method invocation. It is pointed to by the vars register. The execution environment section is used to maintain the operations of the stack itself. It is pointed to by the frame register. The operand stack is used as a work space by bytecode instructions. It is here that the parameters for bytecode instructions are placed, and results of bytecode instructions are found. The top of the operand stack is pointed to by the optop register.

12) What is a root set in garbage collection in JVM?

A) The root set in a Java virtual machine is implementation dependent, but would always include any object references in the local variables and operand stack of any stack frame and any object references in any class variables. Another source of roots are any object references, such as strings, in the constant pool of loaded classes. The constant pool of a loaded class may refer to strings stored on the heap, such as the class name, superclass name, superinterface names, field names, field signatures, method names, and method signatures. Another source of roots may be any object references that were passed to native methods that either hasn’t been "released" by the native method.

13) What are the 2 basic types of garbage collection?

A) Two basic approaches to distinguishing live objects from garbage are reference counting and tracing. Reference counting garbage collectors distinguish live objects from garbage objects by keeping a count for each object on the heap. The count keeps track of the number of references to that object. Tracing garbage collectors actually trace out the graph of references starting with the root nodes. Objects that are encountered during the trace are marked in some way. After the trace is complete, unmarked objects are known to be unreachable and can be garbage collected.

14) Explain reference counting GC?

A) Reference counting was an early garbage collection strategy. In this approach, a reference count is maintained for each object on the heap. When an object is first created and a reference to it is assigned to a variable, the object's reference count is set to one. When any other variable is assigned a reference to that object, the object's count is incremented. When a reference to an object goes out of scope or is assigned a new value, the object's count is decremented. Any object with a reference count of zero can be garbage collected. When an object is garbage collected, any objects that it refers to have their reference counts decremented. In this way the garbage collection of one object may lead to the subsequent garbage collection of other objects.

15) What are the disadvantages of Reference Counting?

A) A disadvantage is that reference counting does not detect cycles: two or more objects that refer to one another. An example of a cycle is a parent object that has a reference to a child object that has a reference back to the parent. These objects will never have a reference count of zero even though they may be unreachable by the roots of the executing program. Another disadvantage of reference counting is the overhead of incrementing and decrementing the reference count each time.

16) Explain Tracing collectors?

A) Tracing garbage collectors trace out the graph of object references starting with the root nodes. Objects that are encountered during the trace are marked in some way. Marking is generally done by either setting flags in the objects themselves or by setting flags in a separate bitmap. After the trace is complete, unmarked objects are known to be unreachable and can be garbage collected.

17) Give an example of tracing algorithm?

A) The basic tracing algorithm is called "mark and sweep." This name refers to the two phases of the garbage collection process. In the mark phase, the garbage collector traverses the tree of references and marks each object it encounters. In the sweep phase, unmarked objects are freed, and the resulting memory is made available to the executing program. In the Java virtual machine, the sweep phase must include finalization of objects.

18) What are compacting collectors?

A) Garbage collectors of Java virtual machines will likely have a strategy to combat heap fragmentation. Two strategies commonly used by mark and sweep collectors are compacting and copying. Both of these approaches move objects on the fly to reduce heap fragmentation. Compacting collectors slide live objects over free memory space toward one end of the heap. In the process the other end of the heap becomes one large contiguous free area. All references to the moved objects are updated to refer to the new location.
Updating references to moved objects is sometimes made simpler by adding a level of indirection to object references. Instead of referring directly to objects on the heap, object references refer to a table of object handles. The object handles refer to the actual objects on the heap. When an object is moved, only the object handle must be updated with the new location. All references to the object in the executing program will still refer to the updated handle, which did not move. While this approach simplifies the job of heap defragmentation, it adds a performance overhead to every object access.

19) What are copying collectors?

A) Copying garbage collectors move all live objects to a new area. As the objects are moved to the new area, they are placed side by side, thus eliminating any free space that may have separated them in the old area. The old area is then known to be all free space. The advantage of this approach is that objects can be copied as they are discovered by the traversal from the root nodes. There are no separate mark and sweep phases. Objects are copied to the new area on the fly, and forwarding pointers are left in their old locations. The forwarding pointers allow the garbage collector to detect references to objects that have already been moved. The garbage collector can then assign the value of the forwarding pointer to the references so they point to the object's new location.

20) Give an example of copy collector?

A) A common copying collector algorithm is called "stop and copy." In this scheme, the heap is divided into two regions. Only one of the two regions is used at any time. Objects are allocated from one of the regions until all the space in that region has been exhausted. At that point program execution is stopped and the heap is traversed. Live objects are copied to the other region as they are encountered by the traversal. When the stop and copy procedure is finished, program execution resumes. Memory will be allocated from the new heap region until it too runs out of space. At that point the program will once again be stopped. The heap will be traversed and live objects will be copied back to the original region. The cost associated with this approach is that twice as much memory is needed for a given amount of heap space because only half of the available memory is used at any time.




21) What is the disadvantage of copy collectors?

A) One disadvantage of simple stop and copy collectors is that all live objects must be copied at every collection. 

22) What are generational collectors and why are they preferred?.

A)    Taking into account two facts that have been empirically observed in most programs in a variety of languages:
  1. A) Most objects created by most programs have very short lives.
  2. Most programs create some objects that have very long lifetimes. A major source of inefficiency in simple copying collectors is that they spend much of their time copying the same long-lived objects again and again.
Generational collectors address this inefficiency by grouping objects by age and garbage collecting younger objects more often than older objects. In this approach, the heap is divided into two or more sub-heaps, each of which serves one "generation" of objects. The youngest generation is garbage collected most often. As most objects are short-lived, only a small percentage of young objects are likely to survive their first collection. Once an object has survived a few garbage collections as a member of the youngest generation, the object is promoted to the next generation: it is moved to another sub-heap. Each progressively older generation is garbage collected less often than the next younger generation. As objects "mature" (survive multiple garbage collections) in their current generation, they are moved to the next older generation.

23) For what type of collection the Generational collection can be used?

A) The generational collection technique can be applied to mark and sweep algorithms as well as copying algorithms. In either case, dividing the heap into generations of objects can help improve the efficiency of the basic underlying garbage collection algorithm.

24) What are Adaptive collectors?

A)  An adaptive garbage collection algorithm takes advantage of the fact that some garbage collection algorithms work better in some situations, while others work better in other situations. An adaptive algorithm monitors the current situation on the heap and adjusts its garbage collection technique accordingly. It may tweak the parameters of a single garbage collection algorithm as the program runs. It may switch from one algorithm to another on the fly. Or it may divide the heap into sub-heaps and use different algorithms on different sub-heaps simultaneously.

25) What is the size of a reference in JVM?
A)  The size is 4 bytes  (32 bits).

26)  What are daemon threads in a JVM?

A) A daemon thread is ordinarily a thread used by the virtual machine itself, such as a thread that performs garbage collection. The application, however, can mark any threads it creates as daemon threads. The initial thread of an application--the one that begins at main()--is a non- daemon thread.

27) When can the JVM terminate?

A) A Java application continues to execute (the virtual machine instance continues to live) as long as any non-daemon threads are still running. When all non-daemon threads of a Java application terminate, the virtual machine instance will exit. If permitted by the security manager, the application can cause its own demise, by invoking the exit() method of class Runtime or System.

28)

Wednesday, August 15, 2012

Natural Order


Natural order

Classes Implementing Comparable
Class
Natural Ordering
Byte
Signed numerical
Character
Unsigned numerical
Long
Signed numerical
Integer
Signed numerical
Short
Signed numerical
Double
Signed numerical
Float
Signed numerical
BigInteger
Signed numerical
BigDecimal
Signed numerical
Boolean
Boolean.FALSE < Boolean.TRUE
File
System-dependent lexicographic on path name
String
Lexicographic
Date
Chronological
CollationKey
Locale-specific lexicographic

java Collection classes


Core implementation classes in Collection Framework

Maps
Sets
Lists
Queues
Utilities
HashMap
HashSet
ArrayList
PriorityQueue
Collections
Hashtable
LinkedHashSet
Vector

Arrays
TreeMap
TreeSet
LinkedList


LinkedHashMap

Stack


Collection and Map Hierarchy




Map Hierarchy

                                    

Tuesday, August 14, 2012

Java Collections FAQ


Java Collections overview: The topic that covers most part of the interview.I will provide questions I faced in my 8 years of experience.

1) Why should be override hashCode() and equals()?.

     Even though not a part of Collections.Most of the collections internally use these methods for their functionality.So this is a stock question asked by almost all interviewers.

equals() is used in HashMap,HashTable for keys and even in Set.If you don't override equals() you cant use them in above collections and many other.If you override equals just be sure that you override hashcode() method also.So to compare 2 objects based on their state and attributes and by not its identity(address bits) we should override equals and hashcode.

2) What is the need to overide equals and hashcode ,both all the time?

    equals and hashcode are bound by a joint contract that specifies if 2 objects are equal then their hashcodes should be identical.Not necessarily the other way round.(From Java specification).

3) What do we need to override hashcode and where is it used in java?.

     Collections such as HashMap,HashSet and HashTable use hashcode to determine how to store and retrieve the object in a collection.

4) How is hashtable implemented?.

     HashTable uses buckets to fill in the objects(keys) and each bucket will be identified by an identifier(hashcode) calculated using the key(object). The process of storing and retrieving objects is a 2 step process.

  1. Find the right bucket (using hashcode()).
  2. Search the bucket for the right element (using equals()).   
5) Can different objects have the same hashcode or can I give 15 as a constant hashcode?

     Yes,you can have a constant hashcode but then the search in collections will degenerate to a linear search(based on implementation).

6) How to implement a good hashcode()?

     Basic logic is to use all the instance variables used in the equals method of the same object.Other than that you can use any legal implementation.

7) If 2 hashcodes are not equal ,then calling equal on those objects return true or false?

    false,If 2 hashcodes are equal then the equals need not return true,but if 2 hashcodes are not equal then the 2 objects also should be not equal.

i.e if x.hashcode() != y.hashcode() then x.equals(y) == false;

8) Can I use transient variables in equals and hashcode?.

     No(but compiler will not complain if you use the same),transient variables after deserialization will be initialized to default values.So the state is not saved on serialization and the computed hashcode will be different on deserialization thus breaking the equlas hashcode contract.

9) Can I change an object state which is used as a key in a hashtable(the key is in the table)?.

    Yes you can change,but changing the variable state or values will change the equality of the objects and on search you might not find the value.So it is always advised to use immutable objects as keys i.e Strings or Wrapper classes.

10)   How to make a class immutable?

    Make a class immutable by following these guidelines :
  • ensure the class cannot be overridden - make the class final, or use static factories and keep constructors private
  • make fields private and final
  • force callers to construct an object completely in a single step, instead of using a no-argument constructor combined with subsequent calls to setXXX methods (that is, avoid the Java Beans convention)
  • do not provide any methods which can change the state of the object in any way - not just setXXX methods, but any method which can change state
  • if the class has any mutable object fields, then they must be defensively copied when passed between the class and its caller
11) What are collections in Java?.

     Collections are datastructure implementations provided by java for the programmer to use directly without reinventing the wheel.Collections can be used for the following operations.
  1. Add objects to the collection
  2. Remove objects from the collection
  3. Find if an object is in the collection.
  4. Retrieve an object from a collection.
  5. Iterate through the collection.
12) What are the key interfaces and classes in Collections Framework?.

     Core Interfaces are
  1. Collection
  2. List
  3. Queue
  4. Set
  5. SortedSet
  6. Map
  7. SortedMap
  8. NavigableSet
  9. NavigableMap 
13) What is the base interface of all collections?

    Collection and Map,

14) Does collection extends any other interface?If so what is the interface?.

   Yes, Iterable is the interface,with only a single method Iterator<T> iterator()

15) Why do we need to extend Iterator interface in Collections?From which version of Java this interface is introduced?.

    Implementing this interface allows an object to be the target of the "foreach" statement.From version 1.5



Note: you can find the hierarchy here Collection and Map Hierarchy
   
Note: Important classes in Collection Framework Collection classes 


16) Name the collection class which implements both List and Queue interfaces?.

      LinkedList [API]

17) Is SortedSet a class or interface?

      Interface [API]

18) What is meant by ordered collections?

      A collection can be an Ordered collection or Unordered collection.A collection is said to be ordered if you can iterate through the collection in a specific order(not-random).Ex:
  • A Hashtable is unordered.
  • ArrayList maintains order based on index.
  • LinkedHashSet maintains the insertion order.
  • TreeSet maintains the natural ordering of the elements.
19) What is a Sorted Collection?

      A Sorted collection means that the order in the collection is determined according to some rules ,known  as sort order.Sorting is based on the properties of the elements not based on the attributes like when the element is added,access time or position.

20) What is Natural order of a Collection and how is different from Sort order?.

     Most of the time the natural order is the sort order.For Strings the natural order is alphabetical order.Some of the classes have already implemented natural ordering.[Natural Order].

We can define the natural order of an object/element by implementing an interface Comparable[API].

21) What is Comparable and why is it used?.

    This interface imposes a total ordering on the objects of each class that implements it.This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.The classes which implement this interface can be directly used in Collections.sort(List)/Arrays.sort(Object[]).

22) Should the natural order of an element be consistent with equals?If yes, then why?.

     Yes, The natural ordering of a class C are said to consistent with equals if obj1.compareTo(obj2) == 0 has the same boolean value as obj1.equals(obj2) for every object obj1 and obj2 of a class C.


     




   

Monday, August 13, 2012

FAQ in Java/J2EE updated daily

Recently one of my colleague had problems loading values from a property file.He analyzed the problem and emphasized that having spaces in the key is creating problems.We have to remove all the spaces in the properties file keys to satisfy him.The problem persisted..

Based on this I would like to provide some basic theory and Q and A on Properties.

1) How to load properties files in Java?.

    Option 1: We can load using absolute file paths or relative file paths.Using absolute file paths is not portable and relative files are resolved based on the JVM directory which users might have control in production.We normally should not use files API to access resources to be platform independent.

   Option 2: Using a property file as a classpath resource.Recommended way to load a resource and is portable.

2) Can we have default values for the properties in java?.

     From the API 'A property list can contain another property list as its "defaults"; this second property list is searched if the property key is not found in the original property list'.So yes but using java API and not in the file.

Properties(Properties defaults) 
          Creates an empty property list with the specified defaults.

or

getProperty(String key, String defaultValue) 
          Searches for the property with the specified key in this property list.


3) Can we use Unicode characters in properties files?

      No,in properties the input/output stream is encoded in ISO 8859-1 character encoding.

4) How can we read Unicode data from properties file?.

      Characters that cannot be directly represented in this encoding can be written using Unicode escapes only a single 'u' character is allowed in an escape sequence. The native2ascii tool can be used to convert property files to and from other character encodings.(From the API).


In Java 1.6 you can use other encodings by using the methods that use Reader/Writer instead of InputStream/OutputStream

Sample: In Java 1.6

Properties props = new Properties();
URL resource = getClass().getClassLoader().getResource("data.properties");         
props.load(new InputStreamReader(resource.openStream(), "UTF8"));




5) How to read properties from properties in java?.

     Use any of the method to get the stream

     ClassLoader.getResourceAsStream ("some/pkg/resource.properties");
     Class.getResourceAsStream ("/some/pkg/resource.properties");
     ResourceBundle.getBundle ("some.pkg.resource");


   
Method
Parameter format
Lookup failure behavior
Usage example
ClassLoader.
getResourceAsStream()
"/"-separated names; no leading "/" (all names are absolute)
Silent (returns null)
this.getClass().getClassLoader()
.getResourceAsStream
("some/pkg/resource.properties")
Class.
getResourceAsStream()
"/"-separated names; leading "/" indicates absolute names; all other names are relative to the class's package
Silent (returns null)
this.getClass()
.getResourceAsStream
("resource.properties")
ResourceBundle.
getBundle()
"."-separated names; all names are absolute;.properties suffix is implied
Throws unchecked
java.util.MissingResourceException
ResourceBundle.getBundle
("some.pkg.resource")
   
     after retrieving the input stream we can load the same into a property object using

     Properties prop = new Properties();
     prop.load(inputStream);

6) Are leading and training spaces considered part of the key in properties?.

     No, from the API Any white space after the key is skipped; if the first non-white space character after the key is '=' or ':', then it is ignored and any white space characters after it are also skipped.

Example : As an example, each of the following three lines specifies the key "Truth" and the associated element value "Beauty"
Truth = Beauty
        Truth:Beauty
 Truth                  :Beauty

7) Can I have only a key in property file without the value?.
     Yes,and the value will be an empty string by default.

8) Can I load properties from an XML file?.

      Yes,we can load an XML file using the method,By default the UTF-8 characterencoding is used,however a specific encoding may be specified if required.
 public void loadFromXML(InputStream in)

 and the DTD should be of the following format

 <?xml version="1.0" encoding="UTF-8"?>
 <!-- DTD for properties -->

 <!ELEMENT properties ( comment?, entry* ) >

 <!ATTLIST properties version CDATA #FIXED "1.0">

 <!ELEMENT comment (#PCDATA) >

 <!ELEMENT entry (#PCDATA) >

 <!ATTLIST entry key CDATA #REQUIRED>

9) Can we save a modified properties to the file?.
     Yes, using store or storeToXML.

10) Is the Properties class thread-safe,can multiple threads access the properties safety?.

     Yes. (From the API: This class is thread-safe: multiple threads can share a single Properties object without the need for external synchronization)

11) What is the super class of Properties?.

    HashTable.

12) Is Properties Serializable or Cloneable?.
    Both.