Java在Java中实例化Short对象 [英] Java instantiate Short object in Java

查看:325
本文介绍了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]

这篇关于Java在Java中实例化Short对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆