JAVA 无法从 int 转换为 short
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37490382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JAVA cannot convert from int to short
提问by Róbert Garai
Hi I am new one here and this is my first question: I have a code with simple aritmetic operator. But it won't works:
嗨,我是新来的,这是我的第一个问题:我有一个带有简单算术运算符的代码。但它不会工作:
int a = 10;
short premennaTypuShort = (short)a; /*retyped variable a into short type and entered into new variable*/
premennaTypuShort = premennaTypuShort - 7;
/*now i am trying to decrease the number but eclipse wrote that cannot convert from int to short.*/
i am trying to decrease the number specified as short but eclipse wrote that cannot convert from int to short. I don't undertand why. so where is the problem ? how can I repair this error?
我试图减少指定为 short 的数字,但 eclipse 写的不能从 int 转换为 short。我不明白为什么。那么问题出在哪里呢?我该如何修复这个错误?
回答by Vinod Kumawat
java is 32 bit. That means that whenever any arithmetic operation is performed it will return in 32 bit value. So, you must cast it to short again, like so:
java是32位的。这意味着无论何时执行任何算术运算,它都会以 32 位值返回。因此,您必须再次将其转换为 short,如下所示:
int a = 10;
short premennaTypuShort = (short)a;
premennaTypuShort =(short)(premennaTypuShort - 7);
回答by Yassin Hajaj
When you're using a byte
, a short
or a char
to perform arithmetic operations involving ints
, there is an automatic promotion to the int
primitive type.
当您使用 a byte
、 ashort
或 achar
执行涉及 的算术运算时ints
,会自动提升到int
原始类型。
Here, you're trying to assign an int
back to a short
.
在这里,你想分配int
回short
。
A solution would be the assignment operator -=
, this will avoid the conversion to an int
一个解决方案是赋值运算符-=
,这将避免转换为int
int a = 10;
short premennaTypuShort = (short)a;
premennaTypuShort-=7;
System.out.println(premennaTypuShort); // 3
WARNING
警告
The assignment operator has a bad side too. Look at the following code.
赋值运算符也有不好的一面。看下面的代码。
short s = Short.MAX_VALUE;
s+=1;
System.out.println(s); // -32768
By adding 1
to Short.MAX_VALUE (32767
), you're overflowing the short and will get unexpected results.
通过添加1
到 Short.MAX_VALUE ( 32767
),你会溢出 short 并且会得到意想不到的结果。
回答by sstan
The problem is that in order to calculate premennaTypuShort - 7
, premennaTypuShort
first needs to be converted to an int
, so the result of the calculation is an int
.
问题是为了计算premennaTypuShort - 7
,premennaTypuShort
首先需要转换为an int
,所以计算的结果是an int
。
This, in turn, means that you are then trying to assign an int
back to a short
variable, which requires an explicit downcast on your part.
反过来,这意味着您正在尝试为变量分配一个int
返回short
值,这需要您进行显式的向下转换。
回答by Arindam
Additive operators (+ and -) are "Numerical Integer Operator". And Numerical Integer Operators always produce a value of type int or long.
Because, any integer operator except shift operator that has at least 1 operand which is of type long is carried out using 64 bit precision and the result would be of type long.
Otherwise the operation is carried out using 32 bit precision and the result would be of type int.
That is why here the expression premennaTypuShort – 7
is producing a result of type int
and to store an int
value to a short
you need to specifically cast it to short
like following, which is known as narrowing.
加法运算符(+ 和 -)是“数值整数运算符”。数字整数运算符总是产生 int 或 long 类型的值。因为,除了至少具有 1 个 long 类型操作数的移位运算符之外的任何整数运算符都使用 64 位精度执行,结果将是 long 类型。否则,操作将使用 32 位精度执行,结果将是 int 类型。这就是为什么这里表达式premennaTypuShort – 7
产生类型的结果int
并将int
值存储到 ashort
您需要专门将其强制转换为short
如下所示,这称为缩小。
premennaTypuShort = (short)(premennaTypuShort - 7)