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

时间:2011-08-12 09:16:55

标签: 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

1 个答案:

答案 0 :(得分:5)

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


15.26.2 Compound Assignment Operators

  

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