Improved Generic Instance Creation in Java 7


Generics was one of the major concepts/new features introduced in Java 5 for achieving compile time safety. While creating an object of generic type, the language mandated both the reference type and actual type to define the generic syntax.

Before Java 7, generic instances were created like this:

Map<Integer, String> myNewMap = new HashMap<Integer, String>();

Java 7 allows the freedom to use only the generic syntax for the reference. In this revised version of the above code snippet, the myNewMap reference type is created without specifying the type information for the actual type.

Map<Integer, String> myNewMap = new HashMap<>();


Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email