What Is the Point of the Diamond Operator (≪≫) in Java

What is the point of the diamond operator () in Java?

The issue with

List<String> list = new LinkedList();

is that on the left hand side, you are using the generic type List<String> where on the right side you are using the raw type LinkedList. Raw types in Java effectively only exist for compatibility with pre-generics code and should never be used in new code unless
you absolutely have to.

Now, if Java had generics from the beginning and didn't have types, such as LinkedList, that were originally created before it had generics, it probably could have made it so that the constructor for a generic type automatically infers its type parameters from the left-hand side of the assignment if possible. But it didn't, and it must treat raw types and generic types differently for backwards compatibility. That leaves them needing to make a slightly different, but equally convenient, way of declaring a new instance of a generic object without having to repeat its type parameters... the diamond operator.

As far as your original example of List<String> list = new LinkedList(), the compiler generates a warning for that assignment because it must. Consider this:

List<String> strings = ... // some list that contains some strings

// Totally legal since you used the raw type and lost all type checking!
List<Integer> integers = new LinkedList(strings);

Generics exist to provide compile-time protection against doing the wrong thing. In the above example, using the raw type means you don't get this protection and will get an error at runtime. This is why you should not use raw types.

// Not legal since the right side is actually generic!
List<Integer> integers = new LinkedList<>(strings);

The diamond operator, however, allows the right hand side of the assignment to be defined as a true generic instance with the same type parameters as the left side... without having to type those parameters again. It allows you to keep the safety of generics with almost the same effort as using the raw type.

I think the key thing to understand is that raw types (with no <>) cannot be treated the same as generic types. When you declare a raw type, you get none of the benefits and type checking of generics. You also have to keep in mind that generics are a general purpose part of the Java language... they don't just apply to the no-arg constructors of Collections!

What is the diamond operator in Java?

Don't worry. It's not an evil. It's feature of Java 7.

The purpose of the diamond operator is to simplify instantiation of generic classes.

For example, instead of

List<Map<Integer,Set<String>>> p = new ArrayList<Map<Integer,Set<String>>>();

with the diamond operator we can write only

List<Map<Integer,Set<String>>> p = new ArrayList<>();

If you want to know more about it and want to use it, please have a quick look here and decide whether it's useful to you or not.

Is it ok to omit diamond operator in recent java versions (8+)?

The correct syntax (for what you want) is:

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

Omitting the diamond operator means an unqualified list, which (under the hood is the same, of course, but) will give you a compiler warning.

Using it unqualified works against the compile-time type-checking system, and is not in accordance with the variable you declare with the diamond operator, but it will compile (and work if you do it right). That's why you will get a warning.

Why diamond operator is used for Type Inference in Java 7?

The definitive answer would have to come from someone who designed that feature, but I'm assuming it's to distinguish this from using raw types, which make the compiler do something different altogether for the sake of compatibility. An expression with a raw type in it is processed subtly differently than one that involves generics, an example is found in this SO question: Generic screws up non-related collection

Diamond operator in raw type context

The diamond is doing what it always does - inferring the generic type from the context, and providing assurance that the constructor call does not compromise type-safety.

Consider this example:

public class GenClass<T> {

GenClass(T t, List<T> list) {}

public static void main(String[] args) {
GenClass m = new GenClass<>(1, new ArrayList<String>()); // Doesn't compile
}
}

This example does not compile, because an appropriate type could not be inferred. If you remove the diamond it does compile because the types of the constructor arguments are the erased versions (Object and List).

In your case, the constructor takes no arguments, so there is nothing really to check. However, using a diamond is a good habit to get into, even when you choose to assign the result of a constructor invocation to Object or a raw type (not that you should use raw types).

Is the diamond operator equivalent to ?

This is Java 7 syntax. The diamond (<>) is a short-hand to ask the Java compiler to fill generic arguments with whatever makes sense in the local context (in this case, it'll be ? super E).

Diamond Operator performance

The generated bytecode is the same. The new diamond operator is purely implemented to save programmers from having to redundantly specify the type twice.



Related Topics



Leave a reply



Submit