6

My Java teacher said it was better to use ++n instead of n++, I am not seeing the logic behind this. Does anyone know?

Mohammad
  • 7,344
  • 15
  • 48
  • 76

8 Answers8

25

++n increments the value and returns the new one.

n++ increments the value and returns the old one.

Thus, n++ requires extra storage, as it has to keep track of the old value so it can return it after doing the increment.

I would expect the actual difference between these two to be negligible these days. I know a lot of compilers will optimize it so they're identical if the return of n++ isn't actually used, though I don't know of Java does that.

Herms
  • 37,540
  • 12
  • 78
  • 101
  • good explanation! in old days, this becomes significant if n is a big object and implements the ++ operator. – trillions Dec 09 '12 at 05:08
  • So their is literally no reason that `n++` is the absolute industry standard? Like, no one uses ++n, unless the code forces them to. – Jonathon Aug 29 '16 at 18:07
  • I wouldn't say that's true. Most of the code I've seen in the last many years uses ++n unless they explicitly need the n++ behavior. – Herms Aug 29 '16 at 18:12
5

Your Java teacher is (probably) refering to the fact that preincrementation is usually a little tiny bit faster than postincrementation. I'm not even sure if that's the case in Java, because I learned about that in C course.

"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil"

Donald Knuth

In everyday pracitice, I would use pre- or postincrementation basing mainly on what makes the most sense in the code.

Mchl
  • 61,444
  • 9
  • 118
  • 120
4

n++ will increment the variable prior to the operation taking place, while ++n only increments the variable after the operation.

So take x = 1
        y = x++
        y = 1
        x = 2 now

So take x = 1 again
        y = ++x
        y = 2
        x = 2 now
Bnjmn
  • 1,973
  • 1
  • 20
  • 34
4

Example:

int n = 10;
int a = n++; // a is 10
int b = n; // b is 11
int c = ++n; // c is 12

The difference is that the value of ++n is n + 1 and the value of n++ is n.

Neither is better, they do the same thing. You shouldn't really rely on the difference between the two in your programs because 1.) quite a few people don't understand the difference and 2.) the same thing can be accomplished far more simply.

Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
  • 1
    People who do not understand that difference should be banned from reading any code. :) – Mchl Jan 20 '11 at 21:39
2

They do different things.

Let's say n = 10. Then n++ increments n but returns 10.

++n increments n and returns 11.

Though I'm not sure which one requires more assembly instructions.

ktm5124
  • 11,861
  • 21
  • 74
  • 119
1

Both increment and decrement operators have two forms which are very important when using the result in the current statement:

  • prefix (++n or --n) increases/decreases the value of a variable before it is used;

  • postfix (n++ or n--) increases/decreases the value of a variable
    after it is used.

let's take an example:

  • Prefix Increment:
int a = 4;
int b = ++a;
System.out.println(a); // 5
System.out.println(b); // 5
  • Postfix Increment
int a = 4;
int b = a++;
System.out.println(a); // 5
System.out.println(b); // 4
Monu Rohilla
  • 1,311
  • 5
  • 20
0

++n adds one to n, then uses it; n++ uses it then adds one. Same applies to --.

Ilya Saunkin
  • 18,934
  • 9
  • 36
  • 50
0

Interesting example that illustrates the difference:

public class Main {
    public static void main(String[] args) {
        for (int i=0; i<5; /*not incrementing here*/ ) 
            System.out.println(i++);
    }
}

output: 0 1 2 3 4

public class Main {
    public static void main(String[] args) {
        for (int i=0; i<5; /*not incrementing here*/ ) 
            System.out.println(++i);
    }
}

output: 1 2 3 4 5

Notice that neither for-loop performs the incrementation since it does not matter there.

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189