Operator precedence in Java

后端 未结 3 1924
轮回少年
轮回少年 2020-12-17 02:01

In one example from http://leepoint.net/notes-java/data/expressions/precedence.html

The following expression

1 + 2 - 3 * 4 / 5

Is

3条回答
  •  执念已碎
    2020-12-17 02:28

    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.

提交回复
热议问题