gpt4 book ai didi

java - instanceof List 和 instanceof List 的区别

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:54:26 28 4
gpt4 key购买 nike

<分区>

我知道我们不能调用 instanceof List<E>因为List<E>不是可具体化的类型。两者 instanceof Listinstanceof List<?>工作;然而 Eclipse IDE 建议使用 instanceof List<?> .

我想知道为什么它建议未绑定(bind)通配符 instanceof List<?>而不是原始调用 instanceof List .是否未绑定(bind)通配符 instanceof List<?>比原始调用有任何优势 instanceof List

提前致谢。

编辑 1:实际上,instanceof Listinstanceof List<?>与编译器在编译时会删除类型相同。但除了 Mena 指出的美观原因之外,它还有其他使用原因吗 instanceof List<?>赞成instanceof List

编辑 2:根据 this entry来自甲骨文:

  1. 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 Listinstanceof List<?>是相同的。

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com