01 / 20
++ --

Increment & Decrement
Operators in Java

Master ++ and -- operators, understand the difference between prefix and postfix behavior, and avoid the traps that catch beginners.

AP Computer Science A Java

What Are Increment & Decrement Operators?

In Java, the increment operator ++ adds \(1\) to a variable, and the decrement operator -- subtracts \(1\) from a variable. They are unary operators: they act on a single operand.

++ Increment
Equivalent to \( x = x + 1 \)
-- Decrement
Equivalent to \( x = x - 1 \)

Why Do They Matter?

Increment and decrement operators appear everywhere in Java: loops, counters, iterators, and algorithm implementations. Understanding them is essential for reading and writing correct code.

1
Loop control: The classic for loop uses i++ to advance the counter each iteration.
2
Concise code: Replace x = x + 1 with a single x++ for cleaner, more idiomatic Java.
3
AP & exam staple: Pre/post behavior is one of the most-tested topics on AP Computer Science A.

Two Forms: Prefix vs Postfix

Each operator can be placed before (prefix) or after (postfix) the variable. Both change the variable by \(1\), but the value of the expression differs.

++x Prefix
Increment first, then return the new value.
x++ Postfix
Return the old value first, then increment.

Core Rule: Prefix ++x

x 5 +1 x (new) 6 return value used 6 ++x : Increment first, then use int y = ++x; // x becomes 6, y gets 6

Core Rule: Postfix x++

x 5 save value used 5 +1 x (after) 6 x++ : Use first, then increment int y = x++; // y gets 5, then x becomes 6

Key Distinction: Pre vs Post

Prefix (++x / --x) Postfix (x++ / x--)
Order Change, then use Use, then change
Expression value New value Old value
Final variable value Same: \( x \pm 1 \) Same: \( x \pm 1 \)
Standalone use Identical effect Identical effect

How to Apply the Operators

1
Identify the variable being incremented or decremented (e.g., count).
2
Check placement: Is the operator before (prefix) or after (postfix) the variable?
3
Ask: Is the value used? If it's a standalone statement, prefix and postfix are the same. If not, continue to step 4.
4
Prefix: increment/decrement first, then substitute the new value into the expression.
5
Postfix: substitute the old value into the expression, then increment/decrement the variable.

Example 1: Simple Assignment

int a = 3; int b = ++a; // prefix increment int c = a++; // postfix increment
1
Line 1: a starts at 3.
2
Line 2: ++a (prefix): a becomes 4 first, then b gets 4.
3
Line 3: a++ (postfix): c gets the old value 4, then a becomes 5.

Example 2: Inside a Print Statement

int x = 10; System.out.println(x++); // prints 10 System.out.println(x); // prints 11 System.out.println(++x); // prints 12
1
Line 2: x++ (postfix): prints 10 (old value), then x becomes 11.
2
Line 3: x is now 11, prints 11.
3
Line 4: ++x (prefix): x becomes 12 first, prints 12.

Example 3: In a For Loop

for (int i = 0; i < 3; i++) { System.out.println(i); } // Output: 0, 1, 2
Loop iteration timeline i = 0 prints 0 i++ i = 1 prints 1 i++ i = 2 prints 2 i++ 3 i < 3 is false loop exits

Example 4: Decrement in Action

int lives = 3; System.out.println(--lives); // prints 2 System.out.println(lives--); // prints 2 System.out.println(lives); // prints 1
1
Line 2: --lives (prefix): lives drops to 2, prints 2.
2
Line 3: lives-- (postfix): prints 2 (old value), then lives drops to 1.
3
Line 4: lives is now 1.

Example 5: Compound Expressions

int n = 5; int result = n++ + ++n;
1
n++: expression uses 5 (old), then n becomes 6.
2
++n: n is currently 6, becomes 7 first, expression uses 7.
3
Result: \( 5 + 7 = 12 \). Final n = 7.

Common Mistakes to Avoid

✕ Confusing Pre and Post
Assuming x++ returns the new value. Remember: postfix returns the old value.
✕ Using on Literals
Writing 5++ or ++10. These operators only work on variables, not constants.
✕ Multiple in One Expression
Expressions like x++ + x++ are hard to read and easy to evaluate incorrectly on exams.

Summary Reference Table

Expression Value of Expression Variable After
++x \( x + 1 \) (new) \( x + 1 \)
x++ \( x \) (old) \( x + 1 \)
--x \( x - 1 \) (new) \( x - 1 \)
x-- \( x \) (old) \( x - 1 \)
x++; (standalone) not used \( x + 1 \)
++x; (standalone) not used \( x + 1 \)

Question 1 of 5

Given int x = 7;, what is the value of y after int y = x++;?

Question 2 of 5

Given int a = 4;, what does System.out.println(++a); print?

Question 3 of 5

What is the value of x after the following code runs?
int x = 10; x++; --x; x--;

Question 4 of 5

Given int m = 2;, what is the value of result after int result = ++m + m++;?

Question 5 of 5

Which of the following will cause a compile error in Java?