【发布时间】:2013-08-21 23:07:41
【问题描述】:
为什么编译器不产生任何错误?我可以在 JLS 的什么地方了解到这种情况?
class Main {
public static void main(String[] args) {
A a = new A();
List<Integer> list = a.getStrings();
}
static class A<X> {
public List<String> getStrings() {
return new ArrayList<String>();
}
}
}
【问题讨论】:
-
这段代码是执行(没有错误)还是只编译?
-
我最近看到了这个问题。基本上,当您使用不带类型参数的
A时,编译器就像A中没有使用泛型一样。这是向后兼容的事情。 -
你使用的是原始的
A类,这对于向后兼容来说并没有错 -
jls 7 --> 4.8 Raw Type