Java中的运算符优先级
在 http://leepoint.net/notes-java/data/expressions 的一个示例中/precedence.html
下面的表达式
1 + 2 - 3 * 4 / 5
被计算为
1 + 2 - 3 * 4 / 5
= (1 + 2) - ((3 * 4) / 5)
= 3 - (12/5)
= 3 - 2 The result of the integer division, 12/5, is 2 .
= 1
然后我看到了另一个例子 http://www.roseindia.net/java/master-java/operator-precedence.shtml
以下表达式
4 + 5 * 6 / 3
计算
4 + (5 * (6 / 3))
,因为我对如何决定哪个表达式有点困惑当涉及 * 和 / 时首先评估。在上面的例子中,两者似乎是有区别的。
第一个示例将 3*5/5
计算为 ((3*4)/5)
而第二个示例将 5*6/3 计算为 (5*(6/3))
我知道 * 和 / 优先于 + 和 - 但是当表达式同时包含 * 和/。为什么上面两个例子显示了不同的方法?其中之一是错误的吗?
编辑
public class ZiggyTest {
public static void main(String[] args) {
System.out.println(4 + (5 * (6 / 3)));
System.out.println(4 + ((5 * 6) / 3));
System.out.println(1 + 2 - (3 * (4 / 5)));
System.out.println(1 + 2 - ((3 * 4) / 5));
}
}
上面的程序产生输出,
14
14
3
1
如果第一个产生相同的输出,为什么最后两个输出不同。
In one example from http://leepoint.net/notes-java/data/expressions/precedence.html
The following expression
1 + 2 - 3 * 4 / 5
Is evaluated as
1 + 2 - 3 * 4 / 5
= (1 + 2) - ((3 * 4) / 5)
= 3 - (12/5)
= 3 - 2 The result of the integer division, 12/5, is 2 .
= 1
Then i saw another example from http://www.roseindia.net/java/master-java/operator-precedence.shtml
The following expression
4 + 5 * 6 / 3
is evaluated as
4 + (5 * (6 / 3))
I am slightly confused as to how it is decided which will be evaluated first when * and / are involved. In the examples above, both seem to be difference.
The first example is evaluating 3*5/5
as ((3*4)/5)
Whereas the second example is evaluating 5*6/3 as (5*(6/3))
I know that * and / have precedence over + and - but what about when the expression includes both * and /. And also why are the above two examples showing different approaches? Is one of them wrong?
Edit
public class ZiggyTest {
public static void main(String[] args) {
System.out.println(4 + (5 * (6 / 3)));
System.out.println(4 + ((5 * 6) / 3));
System.out.println(1 + 2 - (3 * (4 / 5)));
System.out.println(1 + 2 - ((3 * 4) / 5));
}
}
The above program produces the output
14
14
3
1
Why are the last two outputs not the same if the first produced the same output.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这就是为什么我们有规范:)
第 15.7 节 是 Java 语言规范中处理计算顺序的部分,第 15.17 节 指出:
因此,只要有
A op1 B op2 C
并且op1
和op2
都是*
,/< /code> 或
%
它相当于Or,换句话说 - 第二篇链接的文章在他们的示例中是完全错误的。下面是一个证明这一点的示例:
输出:
显示首先发生的是乘法(导致整数溢出),而不是除法(最终会得到 1 的乘法)。
That's why we have specifications :)
Section 15.7 is the section of the Java Language Specification which deals with evaluation order, and section 15.17 states:
So whenever there is
A op1 B op2 C
and bothop1
andop2
are*
,/
or%
it's equivalent toOr to put it another way - the second linked article is plain wrong in their example. Here's an example to prove it:
Output:
That shows the multiplication happening first (leading to integer overflow) rather than the division (which would end up with a multiplication of 1).
你确定吗?
它们产生相同的输出,因为添加括号不会改变结果。对于你的另一个方程,括号实际上确实改变了结果。通过删除我求解的方程中的括号,获得结果的正确路径是第一个。
Are you sure?
They produce the same output because adding the parentheses don't happen to change the result. For your other equation, the parentheses actually do change the result. By removing the parentheses in the equations I solved, the correct path to the result is the first one.
第二个是错误的。请参阅乔恩·斯基特的回答。乘法运算符从左到右进行计算。分组:
应该是
在这种情况下,但是,错误的分组
会产生相同的答案。
The second one is wrong. See Jon Skeet's answer. Multiplicative operators evaluate left to right. The grouping for:
should be
In this case, though, the wrong grouping
yields the same answer.