Operations on byte
, char
and short
are widened to int
unless the compiler can determine the value is in range.
final byte a = 3, b = 4;
byte c = a * b; // compiles
final byte a = 3, b = 40;
byte c = a * b; // compiles
final int a = 3, b = 4;
byte c = a * b; // compiles !!
but
byte a = 3, b = 4;
byte c = a * b; // doesn't compile as the result of this will be `int` at runtime.
final byte a = 30, b = 40;
byte c = a * b; // doesn't compile as the value is too large, will be an `int`
BTW This compiles even though it results in an overflow. :]
final int a = 300000, b = 400000;
int c = a * b; // compiles but overflows, is not made a `long`