<分区>
我知道我们不能调用 instanceof List<E>
因为List<E>
不是可具体化的类型。两者 instanceof List
和 instanceof List<?>
工作;然而 Eclipse IDE 建议使用 instanceof List<?>
.
我想知道为什么它建议未绑定(bind)通配符 instanceof List<?>
而不是原始调用 instanceof List
.是否未绑定(bind)通配符 instanceof List<?>
比原始调用有任何优势 instanceof List
?
提前致谢。
编辑 1:实际上,instanceof List
和 instanceof List<?>
与编译器在编译时会删除类型相同。但除了 Mena 指出的美观原因之外,它还有其他使用原因吗 instanceof List<?>
赞成instanceof List
?
编辑 2:根据 this entry来自甲骨文:
- The type of an instanceof/cast expression is raw
This happens very frequently, as javac forbids instanceof expressions whose target type is a generic type; for casts, the compiler is slightly more permissive since casts to generic type are allowed but a warning is issued (see above). Anyway, the raw type should be replaced by an unbounded wildcard, as they have similar properties w.r.t. subtyping.
Object o = new ArrayList<String>();
List<?> list_string = (List)o;
//same as (List<?>)o boolean b = o instanceof List; //same as o instanceof List<?>
因此,我们可以推断,除了 Mena 所说的美容原因和使用基因的限制外,instanceof List
和 instanceof List<?>
是相同的。