编码:
int r=1;
System.out.println(r + (r=2));
输出是:3。但我期望是4,因为我认为括号内的代码是先执行的?
最佳答案
Official Docs on Operators says
All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left.
所以
+
被评估 left-to-right
, 其中赋值运算符被评估 right to left.
关于Java - 括号和赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17808093/