Tolga Karahan
Tolga Karahan

Reputation: 69

Precedence in Java

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

Upvotes: 3

Views: 486

Answers (5)

Den Isahac
Den Isahac

Reputation: 1525

Unary Operators

However you put the unary operators in an expression, the following table summarize 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
    }
}

Upvotes: 1

Omar
Omar

Reputation: 62

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

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

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1499760

The operator isn't evaluated first. The ordering is:

  • Evaluate LHS (x++) - the result is the original value of x, then x is incremented
  • Evaluate RHS (10) - the result is 10
  • Compare the results of the LHS and RHS

Here's code to demonstrate 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;
    }
}

That prints out 10 then false, because by the time the RHS is evaluated x has been incremented... but the >= operator is still evaluating 9 >= 10 as the result of the expression x++ is the original value of x, not the incremented one.

If you want the result after incrementing, use ++x instead.

Upvotes: 7

Henry
Henry

Reputation: 43728

Your conclusion is not correct. The x++ is evaluated fist, just its value is the value of x before the increment as defined for the postfix increment operation.

Upvotes: 2

Eran
Eran

Reputation: 393771

The relational operator is not evaluated before the increment.

First the operands of the relational operator (x++ and 10) are evaluated.

However, the evaluation of x++ increments x but returns the original value of x, so even though the increment already took place, the value passed to the relational operator is the original value of x.

Upvotes: 4

Related Questions