考虑以下代码段:
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
}
}
...
}
- 在这里,
instanceof
将给出编译错误,因为E
在运行时类型未知。 - 为什么会
(List<E>)o
发出警告?我认为这应该由编译器基于相同的理由报告为错误。
我不确定是否有任何情况下为什么这不会是错误而只能作为警告。