}
public static void foo(List<?> list) {
}
(2)
Object o2 = new List<?>[3]; // 编译居然OK,估计直接当作Raw处理了
Object o3 = new List<? extends Object>[3]; // 报错
上面两段代码,表明了当与Raw类型造型时,<?>在编译器的处理方式的确与<? Extends
Object>有所不同,根据场景它可能被编译器忽略掉泛型信息而直接当作Raw类型,而<?
Extends Object>则不会。
但这种差异,有些吹毛求疵,除了跟Raw类型转换方面存在差异,在语义上两者可以认为是
完全等同的,见:http://bugs.sun.com/view_bug.do?bug_id=6480391
The introduction of the capture conversion simplified a lot of things. One of
the things it did is make "?" equivalent to "? extends Object". Unfortunately,
JLS3 doesn't say they are equivalent.
SUN 的开发人员回复说:
? should be considered equivalent to ? extends Object. I will note this at the
end of the text about bounds for wildcards in 4.5.1.
……
Hence, Foo<?> is semantically equivalent to Foo<? extends Object>
但查了一下发现目前 JLS3中还依然没有增加他说要加的那句注释,见:
http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.5.1
我们暂从语义上认为两者相等。