Java中的运算符优先级

发布于 2024-12-15 09:11:56 字数 1370 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ゝ偶尔ゞ 2024-12-22 09:11:56

我有点困惑,当涉及 * 和 / 时,如何决定先评估哪个

这就是为什么我们有规范:)

第 15.7 节 是 Java 语言规范中处理计算顺序的部分,第 15.17 节 指出:

运算符 *、/ 和 % 称为乘法运算符。它们具有相同的优先级,并且在语法上是左关联的(它们从左到右分组)。

因此,只要有 A op1 B op2 C 并且 op1op2 都是 */< /code> 或 % 它相当于

(A op1 B) op2 C

Or,换句话说 - 第二篇链接的文章在他们的示例中是完全错误的。下面是一个证明这一点的示例:

int x = Integer.MAX_VALUE / 2;        
System.out.println(x * 3 / 3);
System.out.println((x * 3) / 3);
System.out.println(x * (3 / 3));

输出:

-357913942
-357913942
1073741823

显示首先发生的是乘法(导致整数溢出),而不是除法(最终会得到 1 的乘法)。

I am slightly confused as to how it is decided which will be evaluated first when * and / are involved

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:

The operators *, /, and % are called the multiplicative operators. They have the same precedence and are syntactically left-associative (they group left-to-right).

So whenever there is A op1 B op2 C and both op1 and op2 are *, / or % it's equivalent to

(A op1 B) op2 C

Or to put it another way - the second linked article is plain wrong in their example. Here's an example to prove it:

int x = Integer.MAX_VALUE / 2;        
System.out.println(x * 3 / 3);
System.out.println((x * 3) / 3);
System.out.println(x * (3 / 3));

Output:

-357913942
-357913942
1073741823

That shows the multiplication happening first (leading to integer overflow) rather than the division (which would end up with a multiplication of 1).

So尛奶瓶 2024-12-22 09:11:56

你确定吗?

4 + (5 * 6) / 3
4 + 30 / 3
4 + 10
14

4 + 5 * (6 / 3)
4 + 5 * 2
4 + 10
14

它们产生相同的输出,因为添加括号不会改变结果。对于你的另一个方程,括号实际上确实改变了结果。通过删除我求解的方程中的括号,获得结果的正确路径是第一个。

Are you sure?

4 + (5 * 6) / 3
4 + 30 / 3
4 + 10
14

4 + 5 * (6 / 3)
4 + 5 * 2
4 + 10
14

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.

溺ぐ爱和你が 2024-12-22 09:11:56

第二个是错误的。请参阅乔恩·斯基特的回答。乘法运算符从左到右进行计算。分组:

4 + 5 * 6 / 3

应该是

4 + ((5 * 6) / 3).

在这种情况下,但是,错误的分组

4 + (5 * (6 / 3))

会产生相同的答案。

The second one is wrong. See Jon Skeet's answer. Multiplicative operators evaluate left to right. The grouping for:

4 + 5 * 6 / 3

should be

4 + ((5 * 6) / 3).

In this case, though, the wrong grouping

4 + (5 * (6 / 3))

yields the same answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文