My Java teacher said it was better to use ++n instead of n++, I am not seeing the logic behind this. Does anyone know?
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.