Increment & Decrement
Operators in Java
Master ++ and -- operators, understand the difference between prefix and postfix behavior, and avoid the traps that catch beginners.
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.
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.
for loop uses i++ to advance the counter each iteration.x = x + 1 with a single x++ for cleaner, more idiomatic Java.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.
Core Rule: Prefix ++x
Core Rule: Postfix x++
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
count).Example 1: Simple Assignment
a starts at 3.++a (prefix): a becomes 4 first, then b gets 4.a++ (postfix): c gets the old value 4, then a becomes 5.Example 2: Inside a Print Statement
x++ (postfix): prints 10 (old value), then x becomes 11.x is now 11, prints 11.++x (prefix): x becomes 12 first, prints 12.Example 3: In a For Loop
Example 4: Decrement in Action
--lives (prefix): lives drops to 2, prints 2.lives-- (postfix): prints 2 (old value), then lives drops to 1.lives is now 1.Example 5: Compound Expressions
n++: expression uses 5 (old), then n becomes 6.++n: n is currently 6, becomes 7 first, expression uses 7.n = 7.Common Mistakes to Avoid
x++ returns the new value. Remember: postfix returns the old value.5++ or ++10. These operators only work on variables, not constants.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++;?
x++ returns the old value of x (which is 7), then increments x to 8. So y receives 7.Question 2 of 5
Given int a = 4;, what does System.out.println(++a); print?
++a increments a to 5 before the value is passed to println. So it prints 5.Question 3 of 5
What is the value of x after the following code runs?int x = 10; x++; --x; x--;
x++ makes it 11, --x makes it 10, x-- makes it 9. Final value: 9.Question 4 of 5
Given int m = 2;, what is the value of result after int result = ++m + m++;?
++m: m becomes 3, expression uses 3. Then m++: expression uses 3 (old), m becomes 4. Result: \( 3 + 3 = 6 \). Final m = 4.Question 5 of 5
Which of the following will cause a compile error in Java?
++(5) is illegal because ++ requires a variable, not a literal value. The number 5 cannot be incremented: there is no memory location to store the result.