In the first:
byte x = 0; x = 128;
A byte is a signed integral type with a width of 8 bits and can express a range from -128
to +127
.
x = 128
means "assign x
to 128
", and by default 128
is of type int
, so you are trying to assign int
to byte
, which can cause Possible loss of precision
, because int
wider than byte
. To make this work, you need to explicitly specify a value of 128
.
byte x = 0; x = (byte)128;
For your second example, adding values ββto x
is fine, but you are just overflowing the values.
byte x = 0; x += 999L;
source share