Operator precedence in Java

后端 未结 3 1931
轮回少年
轮回少年 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:46

    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.

提交回复
热议问题