Java-generics-no-instanceof

提供:Dev Guides
移動先:案内検索

Javaジェネリック-instanceOfなし

コンパイラは型消去を使用するため、ランタイムは型パラメーターを追跡しないため、実行時に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>) { }