Java precedence

The unary increment and decrement operators of the postfix have more advantage than relational operators according to the precedence table, so why in an expression like this (x ++> = 10) the relational operator is evaluated first and then the variable is incremented?

+3


source to share


5 answers


The operator is not evaluated at first. Order:

  • Evaluate LHS ( x++

    ) - Result is the original value x

    then x

    incremented
  • Estimate RHS ( 10

    ) - Result 10
  • Compare LHS and RHS Results

Here's some code demonstrating that:



public class Test {

    static int x = 9;

    public static void main(String[] args) {
        boolean result = x++ >= showXAndReturn10();
        System.out.println(result); // False
    }

    private static int showXAndReturn10() {
        System.out.println(x); // 10
        return 10;
    }
}

      

This prints 10

, then false

, because at the time of the evaluation the RHS has x

been incremented ... but the operator >=

still evaluates 9 >= 10

as the result of the expression x++

- this is the original value x

, not the incremental one.

If you want to get the result after incrementing, use ++x

.

+7


source


The relation operator is not evaluated until incremented.

The operands of the relational operator ( x++

and 10

) are processed first .



However, the score x++

increments x

but returns the original value x

, so even if the increment has already taken place, the value passed to the relational operator is the original value x

.

+4


source


Your conclusion is wrong. X ++ is evaluated by the hive, its value is simply the x value before incrementing, as defined for the postfix increment operation.

+2


source


Because this is how "++, -" works. If it comes after the variable, the old value is used, then the value is increased, and if it comes before the variable, then the increment occurs first, then the new value is used. So, if you want to use a variable after increasing its value and before checking it, use (++ x> = 10) or increase it without reference, and then check it like:

int x = 0;
x++;
if(x >= 10) {...}

      

+1


source


Unary Operators

However, you put unary operators in an expression, the following table shows its usage.

+----------+-------------------+------------+-----------------------------------------------------------------------------------+
| Operator |       Name        | Expression |                                    Description                                    |
+----------+-------------------+------------+-----------------------------------------------------------------------------------+
| ++       | prefix increment  | ++a        | Increment "a" by 1, then use the new value of "a" in the residing expression.     |
| ++       | postfix increment | a++        | Use the current value of "a" in the residing expression, then increment "a" by 1. |
| --       | prefix decrement  | --b        | Decrement "b" by 1, then use the new value of "b" in the residing expression.     |
| --       | postfix decrement | b--        | Use the current value of "b" in the residing expression, then decrement "b" by 1. |
+----------+-------------------+------------+-----------------------------------------------------------------------------------+

      

Then consider the following java program:

public class UnaryOperators {
    public static void main(String args[]) {
        int n;

        // postfix unary operators
        n = 10;
        System.out.println(n); // prints 10
        System.out.println(n++); // prints 10, then increment by 1
        System.out.println(n); // prints 11

        n = 10;
        System.out.println(n); // prints 10
        System.out.println(n--); // prints 10, then decrement by 1
        System.out.println(n); // prints 9


        // prefix unary operators
        n = 10;
        System.out.println(n); // prints 10
        System.out.println(++n); // increment by 1, then prints 11
        System.out.println(n); // prints 11

        n = 10;
        System.out.println(n); // prints 10
        System.out.println(--n); // decrement by 1, then prints 9
        System.out.println(n); // prints 9
    }
}

      

+1


source







All Articles