【问题标题】:Typecasting with shorthand operators使用速记运算符进行类型转换
【发布时间】:2011-10-25 15:57:49
【问题描述】:
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

【问题讨论】:

    标签: java


    【解决方案1】:

    b>>=2;b = (byte) (b>> 2);


    15.26.2 Compound Assignment Operators

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

    【讨论】:

    • 是的,但是在这里你必须明确地转换成一个字节,而使用速记运算符你不需要。是否有任何与速记运算符相关的规则允许隐式执行此操作。
    猜你喜欢
    • 2011-03-03
    • 1970-01-01
    • 2023-03-29
    • 2014-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-01
    相关资源
    最近更新 更多