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


byte = 1; byte b = 1; byte c = + b; 

throws error: possible loss of precision

byte subt = a_s - a_b;                 ^   required: byte   found:    int 

is behavior has jvm or been defined in java language .

edit : , if defined in java language because of keeping jvm in mind ?

means if java supports byte datatype why operation on byte results int

if java supports byte datatype why operation on byte results int

because that's how java virtual machine designed. there no instruction set perform operation on byte type. rather instruction set int type used operation on boolean, byte, char, , short types.

from jvm spec - section 2.11.1:

a compiler encodes loads of literal values of types byte , short using java virtual machine instructions sign-extend values values of type int @ compile-time or run-time. loads of literal values of types boolean , char encoded using instructions zero-extend literal value of type int @ compile-time or run-time. [..]. thus, operations on values of actual types boolean, byte, char, , short correctly performed instructions operating on values of computational type int.

the reason behind specified in section:

given java virtual machine's one-byte opcode size, encoding types opcodes places pressure on design of instruction set. if each typed instruction supported of java virtual machine's run-time data types, there more instructions represented in byte. [...] separate instructions can used convert between unsupported , supported data types necessary.

for details on instruction sets available various types, can go through table in section.

there table specifying mapping of actual type jvm computational type:


Comments