Increment and Decrement Operators

Naveen AnamaneniUpdated · 5 min read

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:

Token.java▶ Try it
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);
    }
}
Output
Token: 43
After undo: 42

Pre vs Post: When the Value Is Used

The difference appears when the expression's value is used, for example assigned or printed:

x++ uses the old value; ++x uses the new value
FormNameStepsx = 5; y = ...
x++Post-incrementUse the old value, then add 1y = 5, x = 6
++xPre-incrementAdd 1 first, then use the new valuey = 6, x = 6
x--Post-decrementUse the old value, then subtract 1y = 5, x = 4
--xPre-decrementSubtract 1 first, then use the new valuey = 4, x = 4
PrePost.java▶ Try it
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);
    }
}
Output
After y = x++: x = 6, y = 5
After b = ++a: a = 6, b = 6

How it works

  1. Line 4: x++ hands back the old value, 5, which goes into y. Then x becomes 6.
  2. Line 8: ++a makes a 6 first, then hands back 6, which goes into b.
  3. Both variables end at 6; only y and b differ.

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:

Puzzle.java▶ Try it
public class Puzzle {
    public static void main(String[] args) {
        int i = 3;
        int result = i++ + ++i;
        System.out.println("result = " + result + ", i = " + i);
    }
}
Output
result = 8, i = 5
  1. i++ gives 3, and i becomes 4.
  2. ++i makes i 5 and gives 5.
  3. 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

  1. Easy: Predict, then run: int a = 10; int b = a--; int c = --a;. What are a, b and c?
  2. Harder: Predict the result of int i = 2; int r = ++i * i++ + i;, then check it in JShell.

Quick check: Increment and decrement

4 questions
1After int x = 5; int y = x++;, what are x and y?
2After int x = 5; int y = ++x;, what is y?
3What is x after int x = 5; x = x++;?
4Which line doesn't compile?

Key Takeaways

Was this tutorial helpful?

Lesson 44 of 482