使用速记运算符进行类型转换

Lav*_*esh 2 java

byte b=12;

b >>= 2; // Why is this legal? why does it automatically typecasts?

b = b >> 2; // Why is this illegal if the above is legal
Run Code Online (Sandbox Code Playgroud)

Pri*_*ley 5

b>>=2; 和...一样 b = (byte) (b>> 2);


15.26.2复合赋值运算符

形式E1 op = E2的复合赋值表达式等效于E1 =(T)((E1)op(E2)),其中T是E1的类型,除了E1仅被评估一次.