Java: pre-,postfix operator precedences

前端 未结 5 662
-上瘾入骨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条回答
  •  隐瞒了意图╮
    2020-12-03 15:05

    In short,

    Precedence is like preparing the expression to be calculated by putting parentheses. Evaluation comes next from left to right considering each pair of parentheses as a separate operation.

    For example,if i=2 then i+i++ becomes i+(i++) after precedence and evaluates to 2+2 = 4. However, i+++i becomes (i++)+i and evaluates to 2+3 = 5.

    Same to i+(i=5) which evaluates to 2+5 = 7.

    In fact the postfix operators do have higher precedence than prefix operators. For example, i+++++i becomes ((i++)++)+i after precedence which gives a compile error (the second postfix operator needs a variable to operate on but a value is found instead!). If both postfix and prefix operators had had equal precedence then the expression would have become (i++)+(++i) and evaluates to 2+4 = 6.

    If you need more explanation you can compile and run the following code and inspect the examples printed at the output.

    public class TestPrecedence {
        public static void main(String[] str) {
            int i = 0;
            System.out.println("\n");
            i = 2; System.out.println("i = " + i + "\n");
            i = 2; System.out.println("i++ = " + i++ + "\n");
            i = 2; System.out.println("++i = " + ++i + "\n");
            i = 2; System.out.println("i++i = (i++)i TestPrecedence.java:8: error: ')' expected\n"+
                                      "              i++i\n"+
                                      "                 ^\n");
            i = 2; System.out.println("i+-i = i+(-i) = " + (i+-i) + "\n");
            i = 2; System.out.println("++i++ = ++(i++) TestPrecedence.java:12: error: unexpected type\n"+
                                      "                ++i++ \n"+
                                      "                   ^\n"+
                                      "                required: variable\n"+
                                      "                found:    value\n");
            i = 2; System.out.println("i+++++i = ((i++)++)+i TestPrecedence.java:17: error: unexpected type\n"+
                                      "                      i+++++i\n"+
                                      "                       ^\n"+
                                      "                      required: variable\n"+
                                      "                      found:    value\n");
            i = 2; System.out.println("i++ + ++i = " + (i++ + ++i) + "\n");
            i = 2; System.out.println("i+(i=3) = " + (i+(i=3)) + " evaluates left to right\n");
            i = 2; System.out.println("i+i++ precedence yields i+(i++) evaluates to 2+2 = " + (i+i++) + "\n");
            i = 2; System.out.println("i+++i precedence yields (i++)+i evaluates to 2+3 = " + (i+++i) + "\n");
            System.out.println("\n");
        }
    }
    

提交回复
热议问题