Casting type primitives
In Java byte
, they are always signed, and they range from -128
to 127
. When the int
literal is 135
off before byte
, the result is a negative number because the 8th bit is set.
1000 0111
Specifically, the JLS, section 5.1.3 , says:
Narrowing the conversion from a signed integer to an integral type T simply discards all but the n least significant bits, where n is the number of bits used to represent the type T. value will differ from the sign of the input value.
When you create a literal int
like 135
on byte
, it is a narrowing of the primitive conversion.
source to share