因为编译器使用类型擦除,运行时不会跟踪类型参数,所以在Box <Integer>
和Box <String>
之间的运行时差异无法使用instanceOf
运算符进行验证。所以类似下面的代码用法是错误的 ~!
Box<Integer> integerBox = new Box<Integer>();
//Compiler Error:
//Cannot perform instanceof check against
//parameterized type Box<Integer>.
//Use the form Box<?> instead since further
//generic type information will be erased at runtime
if(integerBox instanceof Box<Integer>){ }