最后一行没有任何意义,javac怎么允许呢?
现在, foo()方法也没有意义; 它必须返回null,没有其他值可以以类型安全的方式返回。 因此,运行时的最后一行不会导致任何问题:它为String变量赋值null。
静态地,为什么最后一行编译? ( javac 1.6 u21 b06 )
<X> X foo(List<? super X> list) { return null; } void test() { List<Number> list = ...; String s1 = this.foo(list); // huh? }The last line doesn't make any sense, how could javac allow it?
Now, the foo() method doesn't make sense either; it must return null, there is no other value that can be returned in a type safe manner. Therefore the last line at runtime will not cause any problem: it assign a null to a String variable.
Still, statically, why does the last line compile? (javac 1.6 u21 b06)
最满意答案
代码不会使用下一个JDK版本1.6.0_22-b04进行编译。 错误是:
类型不匹配:无法从Number转换为String
我想你对编译器错误的建议可能是真的。
The code won't compile with the next JDK version 1.6.0_22-b04. Error is:
Type mismatch: cannot convert from Number to String
I guess your suggestion of a compiler bug may be true.
更多推荐
发布评论