Why the type is not displayed when exiting the generics operator

I read that since Java 7, creating Collections specifying the type on the right side, as in the first, is bad, because the compiler can infer the type on the left side.

List<Integer> myList = new ArrayList<Integer>(); 

My question is that when initializing a list like this, the compiler does not find the type, and I get a warning without warning:

 List<Integer> myList = new ArrayList(); 
+7
java generics
source share
4 answers

The compiler does not output a type because you are creating a raw ArrayList . But he is smart enough to warn you that there may be problems with this (unprocessed) object.

The reason for this warning is worth mentioning. Due to the erasure type, the parametric information ( <Integer> ) about the List will completely disappear at runtime when the variable is saved with elements of type Object . Consider this snippet:

 List rawList = new ArrayList(); //raw list rawList.add(new String("hello")); rawList.add(new Double(12.3d)); List<Integer> intList = rawList; // warning! 

This snippet will compile, but will generate a few warnings. Having the original list ( rawList ), you can add any non-primitive type to the list, including String , Double , etc. But if you assign this collection to a list that is listed for storage only , then this is a problem. In Runtime, you will get a ClassCastException when you try to get some element from intList , which should be an Integer , but it is actually String or something else.

In short - don't mix raw types with Generics!

In your case, the compiler would probably infer the type if you used a diamond:

 List<Integer> list = new ArrayList<>(); //ยฏ\_(ใƒ„)_/ยฏ โ†‘โ†‘ 
+12
source share

Because you forgot the angle brackets (called the diamond operator).

 List<Integer> myList = new ArrayList<>(); 

It is syntactically equivalent

 List<Integer> myList = new ArrayList<Integer>(); 

but different from

 List<Integer> myList = new ArrayList(); 

In the third, you say that the right side is a raw type, meaning that an ArrayList can hold every object (not just an integer). Generics simply compile time and compile in a role of type save.

+9
source share

From https://docs.oracle.com/javase/7/docs/technotes/guides/language/type-inference-generic-instance-creation.html :

Note that in order to use automatic type inference during a generic class, you must specify a diamond. [...] The HashMap() constructor refers to the HashMap raw type , not the Map<String, List<String>> .

+7
source share

You can use the diamond operator to output the type from the declaration, as specified in JLS (ยง15.9),.

The expression for creating an instance of a class indicates an instance of the class, possibly followed by arguments of type (ยง4.5.1) or diamond ("<>") if the class instance is generalized (ยง8.1.2), followed by (possibly empty) argument list of the actual value to the constructor.

 List<Integer> myList = new ArrayList<>(); 
+2
source share

All Articles