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,shortusing java virtual machine instructions sign-extend values values of typeint@ compile-time or run-time. loads of literal values of typesboolean,charencoded using instructions zero-extend literal value of typeint@ compile-time or run-time. [..]. thus, operations on values of actual typesboolean,byte,char, ,shortcorrectly performed instructions operating on values of computational typeint.
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
Post a Comment