gpt4 book ai didi

java - java自动装箱出错

转载 作者:行者123 更新时间:2023-11-29 03:22:48 25 4
gpt4 key购买 nike

谁能说出为什么自动装箱不起作用,以及为什么使用构造函数它可以正常工作:

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

您正在尝试分配 intDouble .这些是不兼容的类型。

请注意 boxing conversion

converts expressions of primitive type to corresponding expressions of reference type

所以int会变成 Integer ,但是Integer仍然与 Double 不兼容.

关于java - java自动装箱出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22649223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com