ポストインクリメント演算子とプリインクリメント演算子の間にはかなりの混乱があります。これは、「Robert Sedgewick と Kevin Wayne によるアルゴリズム、第 4 版」の抜粋から簡単に理解できます。
インクリメント/デクリメント演算子: i++ は i = i + 1 と同じで、式に値 i があります。同様に、i-- は i = i - 1 と同じです。コード ++i と --i は、式の値がインクリメント/デクリメントの前ではなく後に取得されることを除いて同じです。
例えば
x = 0;
post increment:
x++;
step 1:
assign the old value (0) value of the x back to x.So, here is x = 0.
step 2:
after assigning the old value of the x, increase the value of x by 1. So,
x = 1 now;
when try to print somthing like:
System.out.print(x++);
the result is x : 0. Because only step one is executed which is assigning
old value of the x back and then print it.
But when, we do operation like this:
i++;
System.out.print(i);
the result is x: 1. which is because of executing Step one at first
statement and then step two at the second statement before printing the
value.
pre increment:
++x;
step 1:
increase the value of x by 1. So, x = 1 now;
step 2:
assign the increased value back to x.
when try to print something like:
System.out.print(++1)
the result is x : 1. Because the value of the x is raised by 1 and then
printed. So, both steps are performed before print x value. Similarly,
executing
++i;
system.out.print(i);
Both steps are executed at statement one. At second statement, just the
value of "i" is printed.