class T {
public static void main(String args[]) {
int x = 0;
x = x++;
System.out.println(x);
}
}

- 158,873
- 26
- 254
- 302

- 33
- 2
-
See if this helps explain http://stackoverflow.com/questions/1094872/is-there-a-difference-between-x-and-x-in-java – Sean Nov 04 '11 at 16:28
-
The answers below give the reason. If you want to know more than the reason and actually need a solution as well, use the prefix operator: `++x`. – G_H Nov 04 '11 at 16:28
-
possible duplicate of [Why does this go into an infinite loop?](http://stackoverflow.com/questions/3831341/why-does-this-go-into-an-infinite-loop) – nawfal Jul 20 '14 at 09:12
11 Answers
Because ++
is the "post increment" operator - it returns the value of the variable before it gets incremented.
The steps are:
- Take the value of
i
(before incrementing it) and remember it - Increment
i
- Assigned the remembered value to
i

- 272,464
- 47
- 358
- 399
-
-
1@user989093 be sure to use the green checkmark on the answer that best suited your question. – p.campbell Nov 04 '11 at 16:50
Because the post increment operator returns a temporal value (x
´s current value) and then increments x.
Try it like this:
class T {
public static void main(String args[]) {
int x = 0;
x = ++x;
System.out.println(x);
}
}

- 43,810
- 29
- 138
- 169
x = x++;
is equivalent to
temp = x;
x = x + 1;
x = temp;
That's why the output is zero.
This operator is known as post increment operator. Other operator related to post increment operator is, per-increment operator.
x = ++x;
If you apply per-increment output will be 1.

- 5,973
- 4
- 37
- 60
Because you've used a postfix ++
operator, which means it's evaluated after the assignment. So first x = x
is evaluated, so x
stays as 0
, and then x++
is evaluated, but the result is not assigned to anything.
You could use the prefix ++
operator to achieve your desired result: x = ++x;
From the docs:
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.

- 164,175
- 21
- 332
- 312
x++
returns zero and then increments x. The assignment x = x++
sets x
(after it has been incremented) to the return value of x++
, which was 0.

- 53,822
- 15
- 101
- 132
because ++ after is add after the program runs. you can fix it by the following two ways
class T {
public static void main(String args[]) {
int x = 0;
x = ++x;
System.out.println(x);
}
}
or
class T {
public static void main(String args[]) {
int x = 0;
x += 1;
System.out.println(x);
}
}

- 73
- 1
- 3
- 6
It's because x++
effectively means, "use the value stored in x
and then increment it". If you want to increment the value before you use it, do ++x
instead.

- 23,085
- 14
- 83
- 107
I don't now that it will make a difference in the output, but you don't need the import and you can change:
x = x++;
to:
x++;

- 600
- 1
- 11
- 28
Showing example of post increment with regular increment, as previous answerers said:
class T {
public static void main(String args[]) {
for (int i = 0; i < 6; i++) {
int x = 0;
x = x+1+i;
System.out.println(x);
}
}
}

- 28,471
- 61
- 196
- 289
The first result of this search is this link.
The reason it behaves as observed is because of the interaction between the operator precedence and the odd "return value" of the ++ operator. The postfix increment happens first (see your favorite copy of the operator precedence chart); but postfix, by definition, returns the previous value of the variable. So that "return value" is not "original x plus one", but instead just "original x" (x, at this point, actually contains the incremented value). This returned value is then assigned to x, which overwrites what the ++ did in the first place. IOW, in the expression x=x++, the incremented value is lost, overwritten by the assignment that happens last.
Google is your friend! :)

- 1,288
- 14
- 18
Explanation of what you are doing:
class T {
public static void main(String args[]) {
int x = 0; // x is zero
x = x++; // assign x to be x (which is 0).
// then increment a non-existant variable by 1
System.out.println(x); //print 0
}
}

- 144,921
- 39
- 244
- 303