Increment and Decrement Operators
A token machine at a bank shows the current number and then moves on to the next. Ask "what's my token?" before or after it moves, and you get different answers. Java's ++ operator works the same way. x++ gives you the old value and then adds one; ++x adds one first and gives you the new value. On its own line the difference doesn't matter, but inside an expression it changes the result, which is why interviewers love it.
++ and -- on Their Own
count++ adds 1 to count; count-- subtracts 1. As a statement on its own line, count++ and ++count do exactly the same thing:
public class Token {
public static void main(String[] args) {
int token = 41;
token++;
++token;
System.out.println("Token: " + token);
token--;
System.out.println("After undo: " + token);
}
}Token: 43
After undo: 42Pre vs Post: When the Value Is Used
The difference appears when the expression's value is used, for example assigned or printed:
| Form | Name | Steps | x = 5; y = ... |
|---|---|---|---|
x++ | Post-increment | Use the old value, then add 1 | y = 5, x = 6 |
++x | Pre-increment | Add 1 first, then use the new value | y = 6, x = 6 |
x-- | Post-decrement | Use the old value, then subtract 1 | y = 5, x = 4 |
--x | Pre-decrement | Subtract 1 first, then use the new value | y = 4, x = 4 |
public class PrePost {
public static void main(String[] args) {
int x = 5;
int y = x++;
System.out.println("After y = x++: x = " + x + ", y = " + y);
int a = 5;
int b = ++a;
System.out.println("After b = ++a: a = " + a + ", b = " + b);
}
}After y = x++: x = 6, y = 5
After b = ++a: a = 6, b = 6How it works
- Line 4:
x++hands back the old value, 5, which goes intoy. Thenxbecomes 6. - Line 8:
++amakesa6 first, then hands back 6, which goes intob. - Both variables end at 6; only
yandbdiffer.
Try it yourself: change line 4 to int y = x--;. What are x and y now?
Predicting Trickier Expressions
Work left to right, and update the variable the moment each ++ or -- is reached:
public class Puzzle {
public static void main(String[] args) {
int i = 3;
int result = i++ + ++i;
System.out.println("result = " + result + ", i = " + i);
}
}result = 8, i = 5i++gives 3, andibecomes 4.++imakesi5 and gives 5.3 + 5 = 8.
Works on Other Types Too
++ and -- work on every numeric type and on char: if char c = 'A';, then c++ makes it 'B'. They include a hidden cast, so byte b = 127; b++; compiles and silently wraps to -128.
Common Mistakes
Writing x = x++;. It leaves x unchanged: the old value is saved, x is incremented, then the saved old value is assigned back. Just write x++;.
Using ++ on a literal or expression. 5++ and (a + b)++ don't compile; ++ needs a variable.
Expecting print(x++) to print the new value. It prints the old value; x changes afterwards.
Interview Corner
What is the difference between ++i and i++?
Both add 1 to i. ++i (pre-increment) adds 1 first and evaluates to the new value; i++ (post-increment) evaluates to the old value and then adds 1. The difference is only visible when the expression's value is used.
What does this print? int i = 3; int r = i++ + ++i;
r is 8 and i is 5. i++ gives 3 and makes i 4; ++i makes i 5 and gives 5; 3 + 5 = 8.
What is the value of x after int x = 5; x = x++; ?
Still 5. The old value 5 is saved, x becomes 6, and then the saved 5 is assigned back to x.
Practice
- Easy: Predict, then run:
int a = 10; int b = a--; int c = --a;. What area,bandc? - Harder: Predict the result of
int i = 2; int r = ++i * i++ + i;, then check it in JShell.
Key Takeaways
Was this tutorial helpful?