JLS will not โpush forward to a larger data typeโ here because it does not perform binary numeric promotion for shift operators. This is described in JLS, section 15.19 .
Unary numeric promotion (ยง5.6.1) is performed separately for each operand. (Binary numeric promotion (ยง5.6.2) is not performed on operands.)
Unary numeric promotion advances byte a to int . The literal 4L does not change, but it must still be an integral type.
This is a compile-time error if the type of each of the operands of the shift operator after unary digital advertising is not a primitive integral type.
Then, only the smallest 5 significant bits are used to shift, to shift a int .
If the advanced type of the left operand is int, then only the five least significant bits of the right operand are used as the offset distance. It is as if the right operand was subjected to the bitwise logical operator AND and (ยง15.22.1) with the mask value 0x1f (0b11111). Therefore, the actually used shear distance is always in the range from 0 to 31 inclusive.
The result of the statement is int , not long , so it can be assigned int without a compiler error.
A shift expression type is an advanced type of left operand.
source share