字节算术:如何减去一个字节变量? [英] Byte arithmetic: How to subtract to a byte variable?
本文介绍了字节算术:如何减去一个字节变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试执行类似操作时遇到错误:
I'm getting an error when I'm trying to do somethink like this:
byte a = 23;
a = a - 1;
编译器给出以下错误:
Test.java:8:可能发现精度损失:必需的int:字节
a = a-1;
^
1错误
The compiler gives this error: Test.java:8: possible loss of precision found : int required: byte a = a - 1; ^ 1 error
铸造无法解决错误...
为什么编译器不让我这样做?
是否需要将变量'a'转换为整数?
Casting doesn't solve the error... Why the compiler don't let me do it? Should I need to transform the variable 'a' into an int?
推荐答案
这样做吧。
a = (byte)(a - 1);
从 a
中减去1整数值。因此,要以字节为单位分配结果,您需要进行显式类型转换。
When you subtract 1 from a
then its integer value. So to get assign the result in byte you need to do explicit type casting.
这篇关于字节算术:如何减去一个字节变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文