Java increment and assignment operator [duplicate]

I am confused about the post ++ and pre ++ operator , for example in the following code

int x = 10;
x = x++;

sysout(x);

will print 10 ?

It prints 10,but I expected it should print 11

but when I do

x = ++x; instead of x = x++;

it will print eleven as I expected , so why does x = x++; doesn't change the the value of x ?


No, the printout of 10 is correct. The key to understanding the reason behind the result is the difference between pre-increment ++x and post-increment x++ compound assignments. When you use pre-increment, the value of the expression is taken after performing the increment. When you use post-increment, though, the value of the expression is taken before incrementing, and stored for later use, after the result of incrementing is written back into the variable.

Here is the sequence of events that leads to what you see:

  • x is assigned 10
  • Because of ++ in post-increment position, the current value of x (i.e. 10) is stored for later use
  • New value of 11 is stored into x
  • The temporary value of 10 is stored back into x, writing right over 11 that has been stored there.

Post Increment(n++) : First execute the statement then increase the value by one.

Pre Increment (++n) : First increase the value by one then execute the statement.

Example:

class IncrementTest{
    public static void main(String[] args){

        System.out.println("***Post increment test***");
        int n = 10;
        System.out.println(n);      // output  10
        System.out.println(n++);    // output  10
        System.out.println(n);      // output  11

        System.out.println("***Pre increment test***");
        int m = 10;
        System.out.println(m);      // output  10
        System.out.println(++m);    // output  11
        System.out.println(m);      // output  11
    }
}

I also stumbled by the trickiness of post-increment operator while preparing for a Java certification exam. Most, even the experienced, think post-increment (++) operator increments the variable after the execution of the statement, just as @bartektartanus stated above. This thinking is good enough for most real world coding, but breaks down if a post/pre-incremented/decremented variable occurs more than once within a statement. Looks like the so-called interview/knowledge test questions promptly check for this loose assumption. It is worth breaking this up. The accepted answer above is not so helpful for a concise, logical mnemonic.

public class Test {
    static int x = 10;

    public static void main(String[] args) {
        print(x++);
    }

    private static void print(int x) {
        System.out.println(x);
        System.out.println(Test.x);
    }
}

Output
10
11

Per the common assumption, the static variable x would be incremented after completion of the call to the print method. But when checked inside the method, it has already been incremented. So, post- increment, decrement operaters submit the variable value to the method or operator to which they are parameters, and increment/decrement the associated variable immediately before the operator/method gets executed. The pre-increment/decrement operation is somewhat less confusing, but again even this happens per method or operator, not the whole statement.

// The OP
int x = 10;
x = x++; // x = 10. x does get incremented but before the assignment operation

int x = 10;
x = x++ + x; // x = 21. x got incremented immediately after the addition operator grabbed its value.

Quoting from The Java tutorials - Assignment, Arithmetic, and Unary Operators:

The increment/decrement operators can be applied before (prefix) or after (postfix) the operand. The code result++; and ++result; will both end in result being incremented by one. The only difference is that the prefix version (++result) evaluates to the incremented value, whereas the postfix version (result++) evaluates to the original value. If you are just performing a simple increment/decrement, it doesn't really matter which version you choose. But if you use this operator in part of a larger expression, the one that you choose may make a significant difference.