Java在Java中实例化Short对象

Java在Java中实例化Short对象

问题描述:

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

I was wondering why we can do:

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来构造构造函数。

Why do we need to take the constructors either with a String or a short.

但你可以这样做:

Short s = 2;

或者这个:

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

或者这个:

Short s = new Short("2");

只要数字在[-2 ^ 15范围内,上述任何一项都可以使用2 ^ 15-1]

Any of the above will work as long as the number is in the range [-2^15, 2^15-1]