Java: pre-,postfix operator precedences

前端 未结 5 671
-上瘾入骨i
-上瘾入骨i 2020-12-03 14:41

I have two similar questions about operator precedences in Java.

First one:

int X = 10;
System.out.println(X++ * ++X * X++); //it pr         


        
5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-03 15:13

    The confusion stems from the fact that the operands are evaluated from left to right. This is done first, before any attention is paid to operator precedence/order of operations.

    This behavior is specified in JLS 15.7.2. Evaluate Operands before Operation

    So X++ * ++X * X++ is first evaluated as 10 * 12 * 12 which yields, as you saw, 1440.

    To convince yourself of this, consider the following:

    X = 10; System.out.println(X++ * ++X);
    X = 10; System.out.println(++X * X++);
    

    If X++ were done first, then ++X second, then multiplication, both should print the same number.

    But they do not:

    X = 10; System.out.println(X++ * ++X); // 120
    X = 10; System.out.println(++X * X++); // 121
    

    So how does this make sense? Well if we realize that operands are evaluated from left to right, then it makes perfect sense.

    X = 10; System.out.println(X++ * ++X); // 120 (10 * 12)
    X = 10; System.out.println(++X * X++); // 121 (11 * 11)
    

    The first line looks like

    X++       * ++X
    10 (X=11) * (X=12) 12
    10        * 12 = 120
    

    and the second

    ++X       * X++
    (X=11) 11 * 11 (X=12)
    11        * 11 = 121
    

    So why are prefix and postfix increment/decrement operators in the table?

    It is true that increment and decrement must be performed before multiplication. But what that is saying is that:

    Y = A * B++
    
    // Should be interpreted as
    Y = A * (B++)
    
    // and not
    Y = (A * B)++
    

    Just as

    Y = A + B * C
    
    // Should be interpreted as
    Y = A + (B * C)
    
    // and not
    Y = (A + B) * C
    

    It remains that the order of the evaluation of the operands occurs left-to-right.


    If you're still not conviced:

    Consider the following program:

    class Test
    {
        public static int a(){ System.out.println("a"); return 2; }
        public static int b(){ System.out.println("b"); return 3; }
        public static int c(){ System.out.println("c"); return 4; }
    
        public static void main(String[] args)
        {
            System.out.println(a() + b() * c());
            // Lets make it even more explicit
            System.out.println(a() + (b() * c()));
        }
    }
    

    If the arguments were evaluated at the time they were needed, either b or c would come first, the other next, and lastly a. However, the program outputs:

    a
    b
    c
    14
    a
    b
    c
    14
    

    Because, regardless of the order that they're needed and used in the equation, they're still evaluated left to right.

    Helpful reading:

    • What are the rules for evaluation order in Java?
    • a += a++ * a++ * a++ in Java. How does it get evaluated?
    • Appendix A: Operator Precedence in Java

提交回复
热议问题