gpt4 book ai didi

Java实例化Java中的Short对象

转载 作者:太空狗 更新时间:2023-10-29 22:56:09 26 4
gpt4 key购买 nike

我想知道为什么我们可以这样做:

Long l = 2L;
Float f = 2f;
Double d = 2d;

甚至

Double d = new Double(2);

不是

Short s = 2s; //or whatever letter it could be

也不

Short s = new Short(2); //I know in this case 2 is an int but couldn't it be casted internally or something?

为什么我们需要使用 String 或 short 来构造函数。

最佳答案

但是您可以这样做:

Short s = 2;

或者这个:

Short s = new Short((short)2);

或者这个:

Short s = new Short("2");

只要数字在 [-2^15, 2^15-1] 范围内,上述任何方法都有效

关于Java实例化Java中的Short对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11526432/

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