I am trying to add two values in a byte array. This is my code:
我试图在字节数组中添加两个值。这是我的代码:
byte[] ars = {3,6,9,2,4};
ars[0] = (byte)ars[0] + (byte)ars[4];
System.out.println( ars[0] );
I get this error on compilation:
我在编译时遇到这个错误:
Main.java:9: possible loss of precision
found : int
required: byte
ars[0] = (byte)ars[0] + (byte)ars[4];
^
1 error
Any help is, as always, much appreciated.
任何帮助,一如既往,非常感谢。
7 个解决方案
#1
10
close, but a little off.
关闭,但有点偏。
ars[0] = (byte)(ars[0] + ars[4]);
keep in mind ars[0]
and ars[4]
are already bytes, so there is no need to cast them to bytes.
请记住ars [0]和ars [4]已经是字节,所以不需要将它们转换为字节。
Instead, cast the result of the summation to a byte.
相反,将求和的结果转换为一个字节。
#2
8
In Java, the sum of two byte
s is an int
. This is because, for instance, two numbers under 127 can add to a number over 127, and by default Java uses int
s for almost all numbers.
在Java中,两个字节的总和是一个int。这是因为,例如,127以下的两个数字可以添加到超过127的数字,并且默认情况下Java使用几乎所有数字的int。
To satisfy the compiler, replace the line in question with this:
要满足编译器,请将以下行替换为:
ars[0] = (byte)(ars[0] + ars[4]);
#3
4
public static void main(String args[]){
byte[] ars = {3,6,9,2,4};
ars[0] = (byte)(ars[0] + ars[4]);
System.out.println( ars[0] );
}
this happens for the reason that java automatically converts expressions which use byte and short variables to int... this is to avoid potential risk of overflow.... as a result even if result may be in the range of byte java promotes type of expression to int
这种情况发生的原因是java自动将使用byte和short变量的表达式转换为int ...这是为了避免潜在的溢出风险....结果即使结果可能在byte java的范围内提升类型表达式为int
#4
3
I cam across this question a while back, and collected all the findings here : http://downwithjava.wordpress.com/2012/11/01/explanation-to-teaser-2/
我暂时回过头看这个问题,并收集了所有的调查结果:http://downwithjava.wordpress.com/2012/11/01/explanation-to-teaser-2/
We all know bytes get converted to ints during an arithmetic operation. But why does this happen? Because JVM has no arithmetic instructions defined for bytes. byte type variables have to be added by first 'numerically promoting' them to 'int' type, and then adding. Why are there no arithmetic instructions for the byte type in JVM? The JVM spec clearly says:
我们都知道在算术运算期间字节会被转换为整数。但为什么会这样呢?因为JVM没有为字节定义的算术指令。必须先添加字节类型变量,首先将它们“数字化地”提升为“int”类型,然后再添加。为什么JVM中的字节类型没有算术指令? JVM规范清楚地说:
The Java virtual machine provides the most direct support for data of type int. This is partly in anticipation of efficient implementations of the Java virtual machine's operand stacks and local variable arrays. It is also motivated by the frequency of int data in typical programs. Other integral types have less direct support. There are no byte, char, or short versions of the store, load, or add instructions, for instance.
Java虚拟机为int类型的数据提供最直接的支持。这部分是为了预期Java虚拟机的操作数堆栈和局部变量数组的有效实现。它还受到典型程序中int数据频率的推动。其他整体类型的直接支持较少。例如,商店,加载或添加指令没有字节,字符或短版本。
#5
2
Please replace that line with the following
请用以下内容替换该行
ars[0] = (byte)(ars[0]+ars[4]);
#6
2
byte[] ars = {3,6,9,2,4};
ars[0] = (byte) (ars[0] + ars[4]);
System.out.println( ars[0] );
This will work, try it
这将有效,尝试一下
#7
2
ars[0] = (byte)(ars[0] + ars[4]);
#1
10
close, but a little off.
关闭,但有点偏。
ars[0] = (byte)(ars[0] + ars[4]);
keep in mind ars[0]
and ars[4]
are already bytes, so there is no need to cast them to bytes.
请记住ars [0]和ars [4]已经是字节,所以不需要将它们转换为字节。
Instead, cast the result of the summation to a byte.
相反,将求和的结果转换为一个字节。
#2
8
In Java, the sum of two byte
s is an int
. This is because, for instance, two numbers under 127 can add to a number over 127, and by default Java uses int
s for almost all numbers.
在Java中,两个字节的总和是一个int。这是因为,例如,127以下的两个数字可以添加到超过127的数字,并且默认情况下Java使用几乎所有数字的int。
To satisfy the compiler, replace the line in question with this:
要满足编译器,请将以下行替换为:
ars[0] = (byte)(ars[0] + ars[4]);
#3
4
public static void main(String args[]){
byte[] ars = {3,6,9,2,4};
ars[0] = (byte)(ars[0] + ars[4]);
System.out.println( ars[0] );
}
this happens for the reason that java automatically converts expressions which use byte and short variables to int... this is to avoid potential risk of overflow.... as a result even if result may be in the range of byte java promotes type of expression to int
这种情况发生的原因是java自动将使用byte和short变量的表达式转换为int ...这是为了避免潜在的溢出风险....结果即使结果可能在byte java的范围内提升类型表达式为int
#4
3
I cam across this question a while back, and collected all the findings here : http://downwithjava.wordpress.com/2012/11/01/explanation-to-teaser-2/
我暂时回过头看这个问题,并收集了所有的调查结果:http://downwithjava.wordpress.com/2012/11/01/explanation-to-teaser-2/
We all know bytes get converted to ints during an arithmetic operation. But why does this happen? Because JVM has no arithmetic instructions defined for bytes. byte type variables have to be added by first 'numerically promoting' them to 'int' type, and then adding. Why are there no arithmetic instructions for the byte type in JVM? The JVM spec clearly says:
我们都知道在算术运算期间字节会被转换为整数。但为什么会这样呢?因为JVM没有为字节定义的算术指令。必须先添加字节类型变量,首先将它们“数字化地”提升为“int”类型,然后再添加。为什么JVM中的字节类型没有算术指令? JVM规范清楚地说:
The Java virtual machine provides the most direct support for data of type int. This is partly in anticipation of efficient implementations of the Java virtual machine's operand stacks and local variable arrays. It is also motivated by the frequency of int data in typical programs. Other integral types have less direct support. There are no byte, char, or short versions of the store, load, or add instructions, for instance.
Java虚拟机为int类型的数据提供最直接的支持。这部分是为了预期Java虚拟机的操作数堆栈和局部变量数组的有效实现。它还受到典型程序中int数据频率的推动。其他整体类型的直接支持较少。例如,商店,加载或添加指令没有字节,字符或短版本。
#5
2
Please replace that line with the following
请用以下内容替换该行
ars[0] = (byte)(ars[0]+ars[4]);
#6
2
byte[] ars = {3,6,9,2,4};
ars[0] = (byte) (ars[0] + ars[4]);
System.out.println( ars[0] );
This will work, try it
这将有效,尝试一下
#7
2
ars[0] = (byte)(ars[0] + ars[4]);