Java-generics-no-instanceof

提供:Dev Guides
2020年6月23日 (火) 01:24時点におけるMaintenance script (トーク | 投稿記録)による版 (Imported from text file)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先:案内検索

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>) { }