【发布时间】:2011-10-19 19:02:29
【问题描述】:
我的问题是对另一个人的问题的跟进:Unbounded wildcard passed to method
他对编译以下代码的原因很感兴趣:
public class ColTest {
static<T> T wildSub(ArrayList<? extends T> holder, T arg){
T t=holder.get(0);
return t;
}
public static void main(String[] args) {
ArrayList<?> list=new ArrayList<Long>(Arrays.asList(2L,3L,7L));
Long lng=1L;
ColTest.wildSub(list, lng);
}
}
我们得出的结论是,诀窍在于编译器推断出 ?作为一个 Object 并通过 Object->Long 的琐碎继承使以下 Long 参数通过。
代码确实使用 Sun/Oracle javac 编译(我使用 1.6.0_26-b03),但在 Eclipse 中没有编译(我使用 Helios),它显示以下编译错误:
The method wildSub(ArrayList<? extends T>, T) in the type ColTest is not applicable for the arguments (ArrayList<capture#2-of ?>, Long)
我的问题是:
这是 Eclipse 使用的 Java 编译器实现中的错误,还是 Java“泛型推理算法”规范中的某种歧义,它是有效的,只是 Eclipse 以不同的方式实现?
【问题讨论】:
标签: java eclipse generics compiler-construction jvm