Based on the fact that a byte
type in java is a signed 8 bit two's complement integer, why doesn't the second way of declaring a byte work?
byte ok = -128;
byte notok = 0b10000000;
My understanding is that 1000000
should be -128
but java indicates the notok
variable above should be an int
and not a byte
0b10000000
is an int
literal (= 0b00000000000000000000000010000000
) which equals to +128
. byte
holds 8 bits and cannot represent +128
. However, you can achieve this as follows:
byte notok = (byte) 0b10000000;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With