The “cast-iron” guarantee that the Java 5 generator provides is that you will never see a ClassCastException exception from casts inserted by the compiler, provided that the compilation has not issued “unverified” warnings.
In real life, you often cannot avoid unverified warnings if your code uses outdated (ungendered) libraries. Throws thrown by the compiler can then throw a ClassCastException, and your task is to prevent this by ensuring that the values returned by the library code are well typed for your declarations.
Otherwise, the situation will not change. Outside of generics, if you use an incompatible type, you will get a ClassCastException just like always.
(Good recommendation for this and other questions about Java Generics and Collections generics.)
Maurice Naftalin
source share