【发布时间】:2017-02-12 22:08:31
【问题描述】:
在决定运算符的操作顺序时,我对以下两种说法感到困惑。
- 语句将从左到右执行。
- 它将按照运算符的优先顺序执行。
以下代码从左到右执行
int i=5;
boolean b = i<5 && ++i<5;//line2
System.out.println(i);//prints 5
//left to right execution in line2.
//< is executed and ++ is not.Even though ++ has higher precedence.
但下面的代码似乎遵循优先顺序:
int a=1,b=1,c=1;
boolean b = a==b&&b==c;//line2: (a==b)&&(b==c)
/* In line2 code would not run from left to right.
First a==b is evaluated then b==c and then && operator.*/
我已经部分地问了这个问题here,但没有得到足够好的解释。 有人可以澄清一下吗?
【问题讨论】:
-
子结果从左到右进行评估。我相信这是您对上一个问题的回答