使用类型转换添加字节,Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9815970/
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
Add bytes with type casting, Java
提问by Figitaki
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.
任何帮助,一如既往,非常感谢。
采纳答案by edthethird
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.
相反,将求和的结果转换为一个字节。
回答by Chetter Hummin
Please replace that line with the following
请用以下内容替换该行
ars[0] = (byte)(ars[0]+ars[4]);
回答by Abhishek Choudhary
ars[0] = (byte)(ars[0] + ars[4]);
回答by Taymon
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 中,两个byte
s的和是一个int
。这是因为,例如,小于 127 的两个数字可以与大于 127 的数字相加,并且默认情况下,Javaint
对几乎所有数字都使用s。
To satisfy the compiler, replace the line in question with this:
为了让编译器满意,请用以下内容替换有问题的行:
ars[0] = (byte)(ars[0] + ars[4]);
回答by gmhk
byte[] ars = {3,6,9,2,4};
ars[0] = (byte) (ars[0] + ars[4]);
System.out.println( ars[0] );
This will work, try it
这会奏效,试试吧
回答by flyinrhyno
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 自动将使用字节和短变量的表达式转换为 int ......这是为了避免潜在的溢出风险......结果即使结果可能在字节范围内 java 促进类型表达式到 int
回答by Abhishek Agarwal
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 数据频率的推动。其他整数类型的直接支持较少。例如,没有字节、字符或短版本的存储、加载或添加指令。