谁能说出为什么自动装箱不起作用,以及为什么使用构造函数它可以正常工作:
int intValue = 12;
Double FirstDoubleValue = new Double(intValue);
Double SecondDoubleValue = intValue; // ==> Error
感谢提前:)
最佳答案
The constructor expects a double
, 原始类型,在这种情况下,通过 widening primitive conversion , 一个 int
可以使用。
然而,在
Double SecondDoubleValue = intValue; // ==> Error
您正在尝试分配 int
到 Double
.这些是不兼容的类型。
converts expressions of primitive type to corresponding expressions of reference type
所以int
会变成 Integer
,但是Integer
仍然与 Double
不兼容.
关于java - java自动装箱出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22649223/