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<>();
Konstantin yovkov
source share