DEV Community

Discussion on: The Behavior of `i = i++` in Java

Collapse
 
elmuerte profile image
Michiel Hendriks

I prefer to use ++i for most cases. As it does one thing instead of two.

My loops always have a ++i incrementor. Even though it does not make much difference. Usually the needless stack operation you get with i++ is optimized away. So there is no performance impact.

Collapse
 
jakebman profile image
jakebman

This is definitely a good habit in C++, where you can overload ++i and i++.

If you are using a custom type, i++ is required to return a temporary object representing the old state of the object, whereas ++i is allowed to 'increment' the i object and return the same object.

Compilers might be able to figure this out, but it's better to not make them do so.