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 .
EDIT : And if it is defined in java language then does it because of keeping jvm in mind ?
Means 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 onboolean
,byte
,char
, andshort
types.From JVM Spec - Section 2.11.1:
The reason behind this is also specified in that section:
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: