Assigning List of Integer Into a List of String

You are not getting any compile time error because by using GenericClass<E> in a raw way :

GenericClass genericClass = new GenericClass();,

you are practically telling the compiler to disable generic type checkings because you don’t care.

So the :

void genericFunction(List<String> stringList)

becomes

void genericFunction(List stringList) for the compiler.

You can try the following : GenericClass<?> genericClass , and you’ll notice immediately that the compiler becomes aware of the generics bad use, and it will show you the error :

The method genericFunction(List<String>) in the type GenericClass<capture#1-of ?> is not applicable for the arguments (List<Integer>)

Also, if you try to get the class of the 2nd position object at runtime:

System.out.println(integerList.get(1).getClass());

, you’ll get an error:
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)