Pre-increment vs Post-increment in Java
Fast recall
Quick revision
Core answer
Understanding of expression evaluation order and operator side effects, usually probed with tricky output-prediction questions
Thinking ++i and i++ always differ, or expecting int b = i++ to give b the incremented value
Walk through a compound expression like int b = ++i + i++ step by step, showing what each operator returns and when i changes
Interview-ready guide
Interview answer
Both operators add one to the variable — the only difference is the order the value comes back. Pre-increment, ++i, increments first and then hands you the new value. Post-increment, i++, hands you the current value first and increments afterward.
So if i is 5 and I write int b = i++, b gets 5 and i becomes 6 — not the 6 most people expect. Switch to ++i and b is 6. What candidates miss is that in a standalone statement, or in a for-loop's update clause, the two are identical, because nothing reads the returned value.
The difference only shows up when you capture it. The classic trap, int b = ++i + i++ with i at 5, gives b twelve and i seven. In real code I keep these out of larger expressions — clarity beats cleverness.
Pre-increment vs Post-increment in Java
Direct Answer
Pre-increment ++i adds 1 first and then returns the new value; post-increment i++ returns the current value first and then adds 1. The difference only shows up when the expression's value is used in an assignment or calculation — as standalone statements they are identical.
Common Mistakes & Pitfalls
- Expecting
int b = i++to give the new value — post-increment returns the old value first, sobgets5wheniis5;ionly becomes6afterwards. - Assuming
++iandi++always differ — as standalone statements (i++;) or in aforloop's update clause they are completely identical, because no one reads the returned value. - Chaining increments in one expression — writing
int b = ++i + i++is valid but a trap people misread; even when you get it right, it is a code-review red flag, so split it into separate statements. - Thinking pre-increment is faster for primitives — for an
intthe JIT produces the same code; the C++ "prefer++i" advice about avoiding a temporary copy does not apply to Java primitives.
Check your understanding
Answer these without looking back at the article.
- What does `int b = i++ + ++i` evaluate to if i starts at 3?
- Is there any performance difference between ++i and i++ for integers in Java?
- Can you use ++ on a final variable?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.