添加字节是因为java语言规则还是因为jvm?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18483470/
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
Is addition of byte converts to int because of java language rules or because of jvm?
提问by Prateek
byte a = 1;
byte b = 1;
byte c = a + b;
Throws error: possible loss of precision
抛出错误:可能会损失精度
byte subt = a_s - a_b;
^
required: byte
found: int
Is this behavior has something to do with jvm or its been defined in java language .
这种行为是否与 jvm 或它是在 java 语言中定义的有关。
EDIT :And if it is defined in java language then does it because of keeping jvm in mind ?
编辑:如果它是用 java 语言定义的,那么是因为记住 jvm 吗?
Means if java supports byte
datatype then why operation on byte
results int
意味着如果java支持byte
数据类型那么为什么operation on byte
结果int
采纳答案by Rohit Jain
if java supports byte datatype then why operation on byte results int
如果java支持字节数据类型那么为什么对字节的操作结果是int
Because that's how the Java Virtual Machine is designed. There is no instruction set to perform operation on a byte type. Rather the instruction set for int
type is used for the operation on boolean
, byte
, char
, and short
types.
因为这就是 Java 虚拟机的设计方式。没有指令集对字节类型执行操作。而对于指令集int
类型用于对操作boolean
,byte
,char
,和short
类型。
From JVM Spec - Section 2.11.1:
A compiler encodes loads of literal values of types
byte
andshort
using Java Virtual Machine instructions that sign-extend those values to values of typeint
at compile-time or run-time. Loads of literal values of typesboolean
andchar
are encoded using instructions that zero-extend the literal to a value of typeint
at compile-time or run-time. [..]. Thus, most operations on values of actual typesboolean
,byte
,char
, andshort
are correctly performed by instructions operating on values of computational typeint
.
编译器对类型的文字值的负载进行编码,
byte
并short
使用 Java 虚拟机指令int
在编译时或运行时将这些值符号扩展为类型值。加载类型的文字值,boolean
并char
使用int
在编译时或运行时将文字零扩展为类型值的指令进行编码。[..]。因此,对实际类型boolean
、byte
、char
和 的值的大多数操作short
都由对计算类型的值进行操作的指令正确执行int
。
The reason behind this is also specified in that section:
这背后的原因也在该部分中说明:
Given the Java Virtual Machine's one-byte opcode size, encoding types into opcodes places pressure on the design of its instruction set. If each typed instruction supported all of the Java Virtual Machine's run-time data types, there would be more instructions than could be represented in a
byte
. [...] Separate instructions can be used to convert between unsupported and supported data types as necessary.
鉴于 Java 虚拟机的一字节操作码大小,将类型编码为操作码会给指令集的设计带来压力。如果每个类型化指令都支持 Java 虚拟机的所有运行时数据类型,那么指令数量将超过
byte
. [...] 可以根据需要使用单独的指令在不受支持和受支持的数据类型之间进行转换。
For the details on what all instruction sets are available for various types, you can go through the table in that section.
有关适用于各种类型的所有指令集的详细信息,您可以浏览该部分中的表格。
There is also a table specifying the mapping of actual type to the JVM computational type:
还有一个表指定了实际类型到 JVM 计算类型的映射:
回答by Suresh Atta
Yes,It's language spec.
是的,这是语言规范。
The addition(+) operator. while the adding, 'a'
is converts(implicitly casts) to int
type,
b as well to type int
. Hence result
is implicitly of type int
.
加法 (+) 运算符。添加时,'a'
将转换(隐式强制转换)为int
type , b 也为 type int
。因此result
是隐式的类型int
。
Same for -
operator too.
同为-
运营商了。
回答by Bernhard Barker
JLS 5.6.2: Binary Numeric Promotioncovers it:
JLS 5.6.2:二进制数字促销涵盖了它:
Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:
If either operand is of type
double
, the other is converted todouble
.Otherwise, if either operand is of type
float
, the other is converted tofloat
.Otherwise, if either operand is of type
long
, the other is converted tolong
.Otherwise, both operands are converted to type
int
.
扩展原语转换(第 5.1.2 节)用于转换一个或两个操作数,如以下规则所指定:
如果任一操作数为 类型
double
,则另一个将转换为double
.否则,如果任一操作数的类型为
float
,则另一个将转换为float
。否则,如果任一操作数的类型为
long
,则另一个将转换为long
。否则,两个操作数都被转换为 type
int
。
回答by Dev Blanked
The compiler is doing the right thing. Because (a + b) can go beyond the maximum value that can be kept in a byte variable. If you tell the compiler a, b values don't change by using the 'final' keyword it wont complain anymore.
编译器正在做正确的事情。因为 (a + b) 可以超出字节变量中可以保存的最大值。如果您告诉编译器 a, b 值不会通过使用 'final' 关键字而改变,它就不会再抱怨了。
final byte a = 1;
final byte b = 1;
byte c = a + b;
回答by Sushil Patel
While doing the arithmetic operations on any operands the result is stored in this form MAX(int,operand1 type,operand2 type,...operandN type)Ex:
byte a=10;
byte b=20;
byte c=a+b;
在对任何操作数进行算术运算时,结果以这种形式存储MAX(int,operand1 type,operand2 type,...operandN type)例如:
byte a=10;
byte b=20;
byte c=a+b;
then result of a+b will be stored in the form of MAX(int,operand1 type,operand2 type,...operandN type) in this case MAX(int,byte,byte) the max value is int which is maximum so c will have the int value but c has been declared as byte, and we can't store int(bigger) value into byte(smaller). the same applies for every arithmetic operator.
那么 a+b 的结果将以 MAX(int,operand1 type,operand2 type,...operandN type) 的形式存储在这种情况下 MAX(int,byte,byte) 最大值是 int 是最大值所以 c将具有 int 值,但 c 已被声明为字节,我们不能将 int(bigger) 值存储到 byte(smaller) 中。这同样适用于每个算术运算符。
that is why the error says error: incompatible types: possible lossy conversion from intto byte
这就是为什么错误说错误:不兼容的类型:从int到 byte 的可能有损转换
回答by Narpat Singh
Compiler is right, declare variables to final or cast to byte:
编译器是对的,将变量声明为 final 或强制转换为 byte:
byte b = 1;
byte c = 22;
byte a = (byte) (b + c);
JAVA : byte+byte = int
JAVA:字节+字节=整数
:)
:)