Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
请检查以下各项:
byte a=1, b=2; b+=a;
是完全合法的。尽管,
byte a=1, b=2; b=b+a;
不被允许。尽管这两者都被认为是等效的。两种分配方式的机制有什么区别吗?
从语言规范:
E1 op= E2 形式的复合赋值表达式等价于 E1 = (T)((E1) op (E2)),其中 T 是 E1 的类型,除了 E1 只计算一次。
这意味着b+=a相当于(byte)(a+b),而不是简单地a+b。后者仍然需要强制转换byte。
b+=a
(byte)(a+b)
a+b
byte