Is addition of byte converts to int because of java language rules or because of jvm?

放肆的年华 提交于 2019-11-26 17:46:51

if java supports byte datatype then why operation on byte results 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.

From JVM Spec - Section 2.11.1:

A compiler encodes loads of literal values of types byte and short using Java Virtual Machine instructions that sign-extend those values to values of type int at compile-time or run-time. Loads of literal values of types boolean and char are encoded using instructions that zero-extend the literal to a value of type int at compile-time or run-time. [..]. Thus, most operations on values of actual types boolean, byte, char, and short are correctly performed by instructions operating on values of computational type 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.

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:

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.

final byte a = 1;
final byte b = 1;
byte c = a + b;

JLS 5.6.2: Binary Numeric Promotion covers it:

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 to double.

  • Otherwise, if either operand is of type float, the other is converted to float.

  • Otherwise, if either operand is of type long, the other is converted to long.

  • Otherwise, both operands are converted to type int.

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.

Same for - operator too.

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;

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.

that is why the error says error: incompatible types: possible lossy conversion from int to byte

Compiler is right, declare variables to final or cast to byte:

byte b =  1;
byte c =  22;
byte a = (byte) (b + c);

JAVA : byte+byte = int

:)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!