Java Diamond Operator

Before Java 7, while using generics we had to supply type parameters to variable types and to their actual types. Now, it has been relieved a bit. And a blank Java diamond operator on the right side of the declaration will work fine.

The diamond operator is denoted with two angles '< >'.

1. Raw Type Declarations (Prior to Java 1.5)

If you have worked on early versions of Java (before 1.5), when generics were not a Java feature, developers have to use raw types declarations and initializations. For example, given below is a HashMap declarations.

List list = new ArrayList();

list.add(new Object());
list.add("string");
list.add(Integer.valueOf(1));

The problem with this approach is that you can put any object type in key and value, and only in runtime, we will get the error if the object is not of the desired type. There is no compile-time safety that can warn developers, which types are allowed and which are not.

String value = list.get(0);   //Runtime Error!

2. Parameterized Types in Java 1.5

JDK 1.5 brought generics. It was a much-needed feature and completely changed the way developers write code. It enabled compile time safety. It helped in reducing runtime errors by a great number.

List<String> list = new ArrayList<String>();

list.add("item 1");
list.add("item 2");
list.add(Integer.valueOf(1));    //Compilation Error!

This syntax solves the problem of compile-time type safety. In fact, the above syntax is good for almost all use cases. In the above example, if you try to add any key or value of a different type, the compiler will give you an error.

You need to fix your code to get through the compiler.

3. Diamond Operator since Java 1.7

Parameterized types solved a few issues but seem heavy due to the same repeated type of information on both sides. We can reduce the syntax if we can provide type information on one side, and another side can detect and apply the type information.

The diamond operator in Java does exactly the same thing. It is also called Elvis operator. Look below at the diamond operator syntax.

List<String> list = new ArrayList<>();

In the above code, the compiler is smart enough to identify that the diamond operator infers to type defined on the left-hand side of the declaration. It applies the type information to the right side object as well. It helps in adding type inference features to Java.

4. Backward Compatibility

Raw types and parameterized types still exist for the sake of backward compatibility. But new compilers will warn if they see raw types. If you compile raw types with Java 5 onward, you will get a warning like this:

ArrayList is a raw type. References to generic type ArrayList<E> should be parameterized.

Happy Learning !!

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode