-3

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?

The Scientific Method
  • 2,374
  • 2
  • 14
  • 25

1 Answers1

4

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 incrementing x.
  • a = ++x, a = x += 1, and a = x = x + 1 are all the value after incrementing x.

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 evaluates x twice, which matters if x 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
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • 1
    Why 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