Example & Tutorial understanding programming in easy ways.

What is the use of Dimond operator feature of java7?

The use of the diamond operator simplifies the use of generics when creating an object. It avoids unchecked warnings in a program, and it reduces generic verbosity by not requiring explicit duplicate specification of parameter types. Instead, the compiler infers the type.

To use the diamond operator:

  1. Create a generic declaration of an object.
  2. Use the diamond operator, <>, to specify the type inference that is to be used

This diamond syntax <> simplify how we instantiate generic type variables. In the previous version of Java when declaring and instantiating generic types we’ll do it like the snippet below:

This make our code simpler and more readable and by using the diamond syntax the compiler will ensure that we have the generic type safe checking available in our code. This will make any error due to type incompatibility captured at the compile time.


Read More →