tags:

views:

560

answers:

8

Possible Duplicates:
What is the difference between i++ and ++i in Java?
Is there a difference between x++ and ++x in java?

All in the question really, have seen many people say oh; why am i doing i++, dont i no ++i is quicker etc, but its just how i was taught

can anyone provide me with a reason why one is better than the other

thanks

+8  A: 

The post-increment operator (i++) needs to keep its previous value around if it is being assigned. If the value isn't assigned, the compiler will optimize away the added instructions.

// Needs to remember the old value to assign it afterwards:
int a = 0;
int b = a++; // b = 0

// Doesn't need to remember since value is incremented before assignment:
int c = ++a; // c = 2 (since a was incremented once already)

// No need to remember the old value:
for (int i=0; i < 10; i++) {
    // ...
}
Ben S
In this case, a decent compiler would copy `a` straight in the case of the `b` allocation, so there would be no additional instructions anyway.
Samir Talwar
Actually, `c` is 2 as you already incremented `a` before ;)
BalusC
Good catch. I fixed the code.
Ben S
+3  A: 

i++ will increment the variable i after it is used whereas ++i will increment the variable i before it is used in context.

Poindexter
+9  A: 

i++ means "increment i, and return its old value".

++i means "increment i, and return its new value".

i++ needs to retain a copy of the old value in order to return it after the increment. This is not generally a very large cost, but it is a cost.

(The issue is rather more interesting in C++, which allows these operators to be overloaded to do expensive operations on large complex types, at which point the difference can begin to matter. C++ coders coming to Java may sometimes exaggerate its importance because of this.)

moonshadow
More importantly, as well as being a small cost it's so transient as to be almost certainly unnoticeable. It's not worth sacrificing readability or anything else for premature optimisation, and this particular "optimisation" is almost guaranteed not to have any measurable impact.
Andrzej Doyle
Assuming that the return value is dropped wouldn't it be a fairly simple optimization for the compiler to convert i++ to ++i during compilation?
tloach
If I remember correctly, if the value of the operand isn't assigned or used, the compiler optimizes away the differences between the pre- and post-increment operators.
Ben S
I think you've nailed it on the head in that the source of this "folk wisdom" is probably C++ coders who are used to worrying about temporaries generated from overloaded postfix increments.
Derrick Turk
Right, not an issue for Java since you can't overload the ++ operator and objects live on the heap. It's a very real issue for C++ programmers.
duffymo
+4  A: 

(++i) is pre-increment and (i++) is post.

Pre-increments will "add the one" before another operation occurs, while post does not.

For example, say you are calling myMethod(int value) as myMethod(i++) where i is 1. One will be sent to myMethod and then i is incremented. myMethod(++i) 2 is sent as i is incremented first.

There are some minor costs associated with post increments as well, as it keeps a reference to the previous value around, but these costs are minor and generally not a consideration.

MarkPowell
+3  A: 
y = i++

is equivalent to

y = i
i = i + 1

.

y = ++i

is equivalent to

i = i + 1
y = i

Therefore if y = 0 and i = 0 then

y = i++
y = 0
i = 1

.

y = ++i
y = 1
i = 1

ETA:

I would agree with the other response about not worrying about performance - worry about the meaning of what you code. This is, as one can see above, pretty easy for a compiler to optimise.

cyborg
+3  A: 

99+ percent of the time the performance difference is non existant in a practical sense. if you are concerned definitely run some benchmarks.

I tend to avoid ++i because it confuses the newbies, but that may not matter in your case.

I would stick to the one that is a) correct for the loop logic in question (if that matters). b) the one that your team has decided on for the sake of consistency.

Matthew Vines
A: 

Whatever flavor you use, use it consistently. As per the discussion above, there is a time and place for each, especially in code like this

y[i++][++i] = x[++i][++j]++ + ++y[i++][++j];

which I absolutely recommend against by the way. It is horribly confusing to all but the most logical mindsets.

Aside from the diversion above, if you use i++, use it consistently and vice versa. At least when someone is reading your code they will understand it and not try to figure out why you keep changing back and forth for no reason. :-) I have seen programmers who switch back and forth for the fun of it and because they can, and in some rare cases have been bitten by nasty bugs because they used it inappropriately in the wrong spot.

Mastering_the_Object
Isn't the result of that statement undefined? IOW, mightn't the first increment of `i` in `i++` take place before or after the immediately following `++i` is executed?
Dan
A: 

if either i++ or ++i are used on a statement of their own, they will have the same behavior.

i++;

or

++i;
HH