My Java teacher said it was better to use ++n instead of n++, I am not seeing the logic behind this. Does anyone know?
-
3I'm sure he probably said it was better to use ++n within a certain context. – George Johnston Jan 20 '11 at 21:27
-
Java and C and C++ and PHP and ... – Joe Jan 20 '11 at 21:43
-
2Duplicate of http://stackoverflow.com/questions/1094872/is-there-a-difference-between-x-and-x-in-java – Zach L Jan 20 '11 at 21:43
-
Probably you teacher doesn't even know why. Where I live, many "programming teachers" don't have a clue. – adrianboimvaser Jan 20 '11 at 21:49
-
1Knowing the difference between them is important, and both have their uses. But one is not better than the other. They are simply different tools. – rfeak Jan 20 '11 at 22:09
8 Answers
++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.

- 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
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.

- 61,444
- 9
- 118
- 120
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

- 1,973
- 1
- 20
- 34
-
I think you mean x = 1; y = x++; (y == 1 && x == 2) evaluates to true, x = 1; y = ++x; (y == 2 && x == 2) evaluates to true – Nathan C. Tresch Dec 11 '14 at 19:54
-
@Bnjmn I think you have n++ and ++n reversed in your written explanation, but your code example is correct. – Inertial Ignorance Apr 22 '18 at 04:58
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.

- 75,757
- 21
- 156
- 151
-
1People who do not understand that difference should be banned from reading any code. :) – Mchl Jan 20 '11 at 21:39
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.

- 11,861
- 21
- 74
- 119
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++
orn--
) 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

- 1,311
- 5
- 20
++n adds one to n, then uses it; n++ uses it then adds one. Same applies to --.

- 18,934
- 9
- 36
- 50
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.

- 79,492
- 20
- 149
- 189