information about the generic type is not stored in the compiled class; it is used only at compile time to check the type's security.
Integer v = display(2);
The Java compiler knows that this method is βgeneralβ, and it returns the same type as its argument, in this case it is Integer, and the result is assigned to Integer, so everything is fine.
String d = display("3");
completely the same trick. But in this case:
Long v = display(2);
Java will tell you that it cannot use, because 2 must be Integer, and Java cannot give it Long, so you need to write:
Long v = display(2L);
As I said, no information is stored in the compiled class, so potentially at runtime you can enter a List<Integer> value of type String , and no exception will be thrown, but when you read this String and try to assign a variable of type Integer , you get it ... Well, pretty hard;)
Maxym source share