原始类型、无界通配符和有界通配符
我有一个简单的问题如下: 这是关于整个问题的一个简单示例:
List a = new ArrayList();
List <?> b;
List <? extends Object> c;
根据 khalid mughal 的 Java SCJP(一本非常好的书!):
a = b; // ok. Widening conversion.
b = a; // ok too. No unchecked warning.
b = c; // ok
c = b; // ok
c=a; // ok but now will issue a unchecked warning. // clause 1
我确实理解,当分配给任何有界通配符引用时,任何原始类型(示例 a)都会出现未经检查的警告(因为原始类型 a 中的内容可以是任何内容)。
我的问题是,既然 c 是最高的上限(?扩展对象),那么 a 是否应该能够在没有警告的情况下分配给 c ?
I have a quick question as below:
Here's a simple examples about this whole issues:
List a = new ArrayList();
List <?> b;
List <? extends Object> c;
According to Java SCJP by khalid mughal (a very good book!):
a = b; // ok. Widening conversion.
b = a; // ok too. No unchecked warning.
b = c; // ok
c = b; // ok
c=a; // ok but now will issue a unchecked warning. // clause 1
I do understand that any raw types (example a) when assigned to any bounded wilcard references, a unchecked warning is issues (since the content in that raw type a could be anything).
My questions is since c is the highest upper bound (? extends objects), shouldn't a be able to assigned to c without that warning?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解你的问题(我真的不认为我理解),根据 此页面:
所以你的问题的答案基本上似乎是“擦除 在存在原始类型的情况下可能会导致未经检查的警告”。据我所知,这种情况最有可能在使用嵌套类型时发生 - 我在擦除的定义中看不到任何可能导致类型更改的其他地方,但也许其他人可以建议这是否是或不是这个的来源。
If I understand your question correctly (and I really don't think I do), there appear to be two instances in which interaction with raw types could cause an unchecked warning to occur, according to this page:
So the answer to your question basically seems to be "erasure could result in unchecked warnings in the presence of raw types". As far as I can tell, this is most likely to occur when nested types are used - I can't see anywhere else in the definition of erasure that would likely cause a type to change, but perhaps someone else can suggest whether that is or isn't the source of this.