The simplistic answer to this question is that 0x98
is not a byte. Java bytes are signed and range from -128 to +127. 0x98
is greater than +127.
But the real explanation starts with the FACT that Java doesn't have byte literals at all. Every integer literal denotes either an int
value or a long
value.
So how does this work then?
byte b = 1;
It works because there is a special rule that allows a compile time constant expression whose type is int
to be assigned to a byte
if and only if the value is in range of byte
. So the above works, but:
byte b2 = 0x98; // ERROR
is rejected because 0x98
is outside of the range of byte
.
And the same rule applies within array initializers.