Consider the following snippet:
class MyClass<E>{
...
public void checkType(Object o){
if(o instanceof List<E>){ //this gives compilation error
List<E> list = (List<E>)o; //this gives unchecked warning
}
}
...
}
- Here, the
instanceof
will give a compilation error as the type ofE
is not known at run-time. - Why does the
(List<E>)o
give a warning? I think this should be reported as an error by the compiler on the same grounds.
I'm not sure if there can be any case why this will not be an error and only qualify as warning.