4
我试图运行下面的代码被JDK8感谢下编译罚款类型推断:JDK8类型推断问题
public static <A,B> B convert(A a) {
return (B) new CB();
}
public static void main(String[] args) {
CA a = new CA();
CB b = convert(a); //this runs fine
List<CB> bl = Arrays.asList(b); //this also runs fine
List<CB> bl1 = Arrays.asList(convert(a)); //ClassCastException here
}
但是,在运行,这将引发ClassCastException异常:CB不能转换为[Ljava.lang .Object,但CB b = convert(a)正常工作。
任何想法为什么?
等等,转换甚至在做什么?它的参数被忽略,所以传入的内容并不重要。 – Carcigenicate
您使用哪个JRE版本?我在1.8.0_92上得到了一个操作数栈上的java.lang.VerifyError:Bad type。顺便说一句,使用类型见证对我有用:'List bl1 = Arrays.asList(Test。 convert(a));' –
@StefanZobel确实有效,但我用了150次左右的用法并且手动更改并不容易 - 我希望JDK8能够自动进行推理 –