java - Multiplying two bytes -
can explain me why can't multiply 2 bytes in way?
byte = 1; byte b = 1; byte c = a*b;
or
byte = 1; byte b = 1; short c = a*b;
why have in way?
byte = 1; byte b = 1; byte c = (byte)(a*b);
or
byte = 1; byte b = 1; int/double/float/long c = a*b;
when performing math byte
s, binary numeric promotion takes place, specified jls, section 5.6.2.
when operator applies binary numeric promotion pair of operands, each of must denote value convertible numeric type, following rules apply, in order:
if operand of reference type, subjected unboxing conversion (§5.1.8).
widening primitive conversion (§5.1.2) applied convert either or both operands specified following rules:
if either operand of type double, other converted double.
otherwise, if either operand of type float, other converted float.
otherwise, if either operand of type long, other converted long.
otherwise, both operands converted type int.
(emphasis mine)
that forces assign type @ least wide int
or cast byte
.
Comments
Post a Comment