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:
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:
List
and the Map
on both left and the right
side. By using the diamond syntax the compiler will infer the type of
the right side expression argument automatically. So in Java 7 we can
write the above code snippet like this: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.
Core Java Interview Questions
Object Oriented Programming(OOP) Questions and Answers
Collections Interview Questions
Java Exceptions Interview Questions
Java Threads Interview Questions
Collection framework in java interview questions
oops concepts with example in java
Java Serialization Interview Questions
Top 20 Core Java Interview Questions with Answers
String Interview Question in java With Example
synchronization interview questions
Java Reflection Interview question
Java Executor Framework (JDK 1.5) Interview Questions
JDK 1.7 interview Questations and Answers
Java I/O interview question with example