Java在Java中实例化Short对象

编程入门 行业动态 更新时间:2024-10-25 06:30:11
本文介绍了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对象

本文发布于:2023-08-04 17:39:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1298302.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实例   对象   Java   Short

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!