I know that pre and post increment(like ++i, i++, --i, i--
) used for shorthand, getting value before increment or after increment. this can be achieved with other methods too . but, Do they have another additional advantage?

- 2,374
- 2
- 14
- 25
-
Are you familiar with the `Unary` & `Binary` Operators? – user2004685 Aug 21 '18 at 20:17
-
@user2004685 yes – The Scientific Method Aug 21 '18 at 20:18
-
3Note you can also do the same thing with `i += 1` or `i -= 1`. – FredK Aug 21 '18 at 20:19
-
3No, there shouldn't be any other advantage after the JIT has the chance to work over the code. They'll get compiled down to the same thing. – Louis Wasserman Aug 21 '18 at 20:19
-
@LouisWasserman while you code, is there any situation you prefer i++ over i=i+1, for some advantage except shorthand – The Scientific Method Aug 21 '18 at 20:23
-
I think one advantage is that many C programmers were familiar with pre- and post-increment, so having them available in Java eased adoption. They're also a handy and compact shorthand once you get used to them. – markspace Aug 21 '18 at 20:23
-
@FredK i know but while you code, is there any situation you prefer i++ over i=i+1, for some advantage except shorthand – The Scientific Method Aug 21 '18 at 20:25
-
1@TheScientificMethod Array indexing is a common usage for pre- and post-increment `s[i++] = m[j++];` – markspace Aug 21 '18 at 20:25
-
@markspace Well you can do it like also s[i+1] = m[j+1], still no advantage except shorthand – The Scientific Method Aug 21 '18 at 20:29
-
4Those are not equivalent because the values of `i` or `j` wouldn't update. – Mick Mnemonic Aug 21 '18 at 20:30
-
1@TheScientificMethod Right, what Mick said. What you wrote and what I wrote are totally different. Your code computes `i+1` but does nothing else. `i++` uses `i`'s current value (not i+1) and then afterwards increments the value of `i` for all future calculations. – markspace Aug 21 '18 at 21:14
1 Answers
So, what are the differences between:
x++
++x
x += 1
x = x + 1
As mentioned by Louis Wasserman in a comment:
There shouldn't be any other advantage after the JIT has the chance to work over the code. They'll get compiled down to the same thing.
This is however only a true statement if the 4 are used as standalone statements. All 4 of them can also be used as an expression, in which case they are not all the same:
a = x++
is the value before incrementingx
.a = ++x
,a = x += 1
, anda = x = x + 1
are all the value after incrementingx
.
Also, x = x + 1
evaluates x
twice, while the other 3 only evaluates it once. For a simple x
that makes no difference, but for e.g. an array lookup a[k]
that makes a difference, especially if "k" can change.
Example:
// setup
int[] a = { 5, 15, 25 };
int k = 0;
// result of each if run right after setup
a[k++]++; // k = 1, a = [6, 15, 25]
++a[k++]; // k = 1, a = [6, 15, 25]
a[k++] += 1; // k = 1, a = [6, 15, 25]
a[++k]++; // k = 1, a = [5, 16, 25]
a[k+=1]++; // k = 1, a = [5, 16, 25]
a[k=k+1]++; // k = 1, a = [5, 16, 25]
a[k++] = a[k++] + 1; // k = 2, a = [16, 15, 25]
Now, that last one is a doozie. For a long explanation of how that works, see this answer, but it evaluates left-to-right:
k = 0; a[k++] = a[k++] + 1;
k = 1; a[0] = a[k++] + 1;
k = 2; a[0] = a[1] + 1;
k = 2; a[0] = 15 + 1;
k = 2; a[0] = 16;
To recap:
- There is no difference if run as a statement, and
x
has no side-effects (very common). - As an expression, the
x++
post-increment evaluates to a different value. x = x + 1
evaluatesx
twice, which matters ifx
has side-effects (rare).
Other than that, it is a matter of choice which to use.
Bytecode
For those curious about bytecode, using Eclipse compiler, if "x" is a simple variable, all 4 as statements compiles to
iinc 1, 1
As expressions with simple variables, e.g. y = x++
, they compile differently:
// y = x++;
iload_2
iinc 2, 1
istore_1
// y = ++x; y = x += 1; y = x = x + 1;
iinc 2, 1
iload_2
istore_1
With array:
// y = a[k]++;
aload_1
iload_2
dup2
iaload
dup_x2
iconst_1
iadd
iastore
istore_3
// y = ++a[k]; y = a[k] += 1;
aload_1
iload_2
dup2
iaload
iconst_1
iadd
dup_x2
iastore
istore_3
// y = a[k] = a[k] + 1;
aload_1
iload_2
aload_1
iload_2
iaload
iconst_1
iadd
dup_x2
iastore
istore_3

- 154,647
- 11
- 152
- 247
-
1Why would `x` be evaluated multiple times? Could you print out the bytecode from your examples? – Kayaman Aug 21 '18 at 20:50
-
@Kayaman In `x = x + 1`, the sub-expression "x" is evaluated twice because the language defines it so. As a simple variable, e.g. "x" is `i`, as in `i = i + 1`, you'd never notice, but if "x" is `a[k++]`, it makes a huge difference, as explicitly covered in my answer. – Andreas Aug 21 '18 at 20:53
-
Ah yes of course with a more complicated expression it makes sense. I was just wondering whether that double evaluation would show up in the bytecode. – Kayaman Aug 21 '18 at 20:54
-
@Kayaman Added examples of how bytecode is different in some cases. With array, the double evaluation shows up. – Andreas Aug 21 '18 at 21:13
-
Thanks. So `y = ++x; y = x += 1; y = x = x + 1;` are equivalent in the bytecode, the more complex expressions obviously not. – Kayaman Aug 21 '18 at 21:14
-
@Andreas while you actually code, was there any situation you prefer `i++` over `i=i+1` or `x += 1`, to reduce multiple evaluations . `x ` is a variable in the right hand, what do you mean `x` is evaluated multiple times? – The Scientific Method Aug 22 '18 at 04:08
-
@TheScientificMethod The reason I used "x" instead of `i`, is because "x" is a placeholder which *can* be just a variable, but can also be an array cell, as illustrated in the answer. Try reading the answer in full. --- I **always** prefer `i++` over `i += 1` and `i = i + 1`, especially in `for` loops, because of brevity and it shows intent, i.e. increment to *next* value, for integers anyway. I probably wouldn't use it for `double` (or `float`), but then again, I can't remember ever having to *increment* a floating-point variable by 1. – Andreas Aug 22 '18 at 05:20