The Java Generics is based on Erasure, which means the type check is only done at compile time, and at runtime the type information is erased.
The following code will throw an ClassCastException
, because there is no way the JVM can check the cast at runtime.
At runtime, the list
object’s type is just List
. When using type parameter E
to cast the object
, the element’s type is just Object
and it’s legal to add an Object
to List
. But when trying to get an element from the strings, the cast (generated by compiler) throws an ClassCastException
as an Integer
cannot be casted into a String
.