Surprisingly, when you perform operations on bytes the computations will be done using int
values, with the bytes implicitly cast to (int)
first. This is true for short
s as well, and similarly float
s are up-converted to double
when doing floating-point arithmetic.
The second snippet is equivalent to:
byte someVar;
someVar = (int) someVar - 3;
Because of this you must cast the result back to (byte)
to get the compiler to accept the assignment.
someVar = (byte) (someVar - 3);