Java - 括号和赋值 [英] Java - parentheses and assignment

查看:159
本文介绍了Java - 括号和赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

int r=1;
System.out.println(r + (r=2));

输出为:3。但我预计4,因为我认为括号内的代码是先执行的?

The output is: 3. But I expected 4 because I thought the code inside the parentheses is executed first?

推荐答案

关于运算符的官方文档说


评估除赋值运算符之外的所有二元运算符从左到右;赋值运算符从右到左进行求值。

All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left.

所以 + 被评估从左到右,其中从右到左评估赋值运算符

So + is evaluated left-to-right,where as assignment operators are evaluated right to left.

这篇关于Java - 括号和赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆