Java Tutorial
🔍

Increment & Decrement Operators

Increment & Decrement Operators

Increment (++) and decrement (--) operators are special shorthand operators in Java that increase or decrease a variable's value by 1. Understanding the difference between pre and post forms is crucial for writing correct code.

Goal: Master increment and decrement operators, understand the critical difference between prefix and postfix forms, and avoid common mistakes.

Key Insight: The position of ++ or -- matters! ++x increments THEN returns the value. x++ returns the value THEN increments. This subtle difference causes many bugs in real code.

What are Increment/Decrement Operators?

Increment (++): Increases value by 1
Decrement (--): Decreases value by 1

Java
1int x = 5; 2x++; // x becomes 6 3x++; // x becomes 7 4x--; // x becomes 6

Equivalent Operations:

Java
1// These are all equivalent: 2x++; // Increment 3x = x + 1; // Add 1 4x += 1; // Add 1 shorthand

Two Forms: Prefix vs Postfix

Prefix Form (++x or --x)

Prefix: Increment/decrement FIRST, THEN use the value.

Java
1int x = 5; 2int y = ++x; // x becomes 6 first, then y gets 6 3 4System.out.println("x = " + x); // Output: x = 6 5System.out.println("y = " + y); // Output: y = 6

Step by Step:

  1. Increment x from 5 to 6
  2. Assign the new value (6) to y

Postfix Form (x++ or x--)

Postfix: Use the value FIRST, THEN increment/decrement.

Java
1int x = 5; 2int y = x++; // y gets 5 first, then x becomes 6 3 4System.out.println("x = " + x); // Output: x = 6 5System.out.println("y = " + y); // Output: y = 5

Step by Step:

  1. Assign current value (5) to y
  2. Increment x from 5 to 6

Increment Operator (++)

Pre-increment (++x)

Java
1public class PreIncrementDemo { 2 public static void main(String[] args) { 3 int x = 10; 4 5 System.out.println("Initial x: " + x); // Output: 10 6 7 int y = ++x; // Increment x first, then assign to y 8 9 System.out.println("After ++x:"); 10 System.out.println("x = " + x); // Output: 11 11 System.out.println("y = " + y); // Output: 11 12 } 13}

Execution Order:

  1. x is incremented to 11
  2. The new value (11) is assigned to y

Post-increment (x++)

Java
1public class PostIncrementDemo { 2 public static void main(String[] args) { 3 int x = 10; 4 5 System.out.println("Initial x: " + x); // Output: 10 6 7 int y = x++; // Assign to y first, then increment x 8 9 System.out.println("After x++:"); 10 System.out.println("x = " + x); // Output: 11 11 System.out.println("y = " + y); // Output: 10 12 } 13}

Execution Order:

  1. The current value (10) is assigned to y
  2. Then x is incremented to 11

Side-by-Side Comparison

Java
1public class IncrementComparison { 2 public static void main(String[] args) { 3 // Pre-increment 4 int a = 5; 5 int b = ++a; 6 System.out.println("Pre-increment:"); 7 System.out.println("a = " + a + ", b = " + b); // a = 6, b = 6 8 9 // Post-increment 10 int x = 5; 11 int y = x++; 12 System.out.println("\nPost-increment:"); 13 System.out.println("x = " + x + ", y = " + y); // x = 6, y = 5 14 } 15}

Decrement Operator (--)

Pre-decrement (--x)

Java
1public class PreDecrementDemo { 2 public static void main(String[] args) { 3 int x = 10; 4 5 System.out.println("Initial x: " + x); // Output: 10 6 7 int y = --x; // Decrement x first, then assign to y 8 9 System.out.println("After --x:"); 10 System.out.println("x = " + x); // Output: 9 11 System.out.println("y = " + y); // Output: 9 12 } 13}

Post-decrement (x--)

Java
1public class PostDecrementDemo { 2 public static void main(String[] args) { 3 int x = 10; 4 5 System.out.println("Initial x: " + x); // Output: 10 6 7 int y = x--; // Assign to y first, then decrement x 8 9 System.out.println("After x--:"); 10 System.out.println("x = " + x); // Output: 9 11 System.out.println("y = " + y); // Output: 10 12 } 13}

Decrement Comparison

Java
1public class DecrementComparison { 2 public static void main(String[] args) { 3 // Pre-decrement 4 int a = 10; 5 int b = --a; 6 System.out.println("Pre-decrement:"); 7 System.out.println("a = " + a + ", b = " + b); // a = 9, b = 9 8 9 // Post-decrement 10 int x = 10; 11 int y = x--; 12 System.out.println("\nPost-decrement:"); 13 System.out.println("x = " + x + ", y = " + y); // x = 9, y = 10 14 } 15}

In Expressions

Prefix in Expressions

Java
1public class PrefixExpressions { 2 public static void main(String[] args) { 3 int x = 5; 4 int result = ++x * 2; // x becomes 6, then 6 * 2 = 12 5 6 System.out.println("x = " + x); // Output: x = 6 7 System.out.println("result = " + result); // Output: result = 12 8 } 9}

Evaluation:

  1. ++x increments x to 6
  2. 6 * 2 evaluates to 12

Postfix in Expressions

Java
1public class PostfixExpressions { 2 public static void main(String[] args) { 3 int x = 5; 4 int result = x++ * 2; // 5 * 2 = 10, then x becomes 6 5 6 System.out.println("x = " + x); // Output: x = 6 7 System.out.println("result = " + result); // Output: result = 10 8 } 9}

Evaluation:

  1. Current value (5) is used in expression: 5 * 2 = 10
  2. Then x is incremented to 6

Complex Expression Examples

Java
1public class ComplexExpressions { 2 public static void main(String[] args) { 3 int a = 5; 4 int b = 10; 5 6 // Using pre-increment 7 int result1 = ++a + ++b; // a=6, b=11, then 6+11=17 8 System.out.println("a=" + a + ", b=" + b + ", result1=" + result1); 9 // Output: a=6, b=11, result1=17 10 11 // Reset 12 a = 5; 13 b = 10; 14 15 // Using post-increment 16 int result2 = a++ + b++; // 5+10=15, then a=6, b=11 17 System.out.println("a=" + a + ", b=" + b + ", result2=" + result2); 18 // Output: a=6, b=11, result2=15 19 20 // Reset 21 a = 5; 22 b = 10; 23 24 // Mixed 25 int result3 = ++a + b++; // a=6, 6+10=16, then b=11 26 System.out.println("a=" + a + ", b=" + b + ", result3=" + result3); 27 // Output: a=6, b=11, result3=16 28 } 29}

Standalone vs In-Expression

Standalone Usage

When used alone, prefix and postfix are equivalent:

Java
1int x = 5; 2x++; // x becomes 6 3// Same as: 4++x; // x becomes 6 5 6// Both just increment x, no difference
Java
1public class StandaloneDemo { 2 public static void main(String[] args) { 3 int count = 0; 4 5 count++; // count = 1 6 ++count; // count = 2 7 count++; // count = 3 8 9 System.out.println("Final count: " + count); // Output: 3 10 } 11}

In Expressions - Big Difference!

When used in expressions, they behave differently:

Java
1public class ExpressionDifference { 2 public static void main(String[] args) { 3 int x = 5; 4 int y; 5 6 // Postfix - returns old value 7 y = x++; 8 System.out.println("Postfix: x=" + x + ", y=" + y); 9 // Output: x=6, y=5 10 11 // Reset 12 x = 5; 13 14 // Prefix - returns new value 15 y = ++x; 16 System.out.println("Prefix: x=" + x + ", y=" + y); 17 // Output: x=6, y=6 18 } 19}

With Loops

For Loop Examples

Java
1public class LoopExamples { 2 public static void main(String[] args) { 3 // Standard for loop - postfix is common 4 for (int i = 0; i < 5; i++) { 5 System.out.print(i + " "); // Output: 0 1 2 3 4 6 } 7 System.out.println(); 8 9 // Prefix works the same in this context 10 for (int i = 0; i < 5; ++i) { 11 System.out.print(i + " "); // Output: 0 1 2 3 4 12 } 13 System.out.println(); 14 15 // Decrement loop 16 for (int i = 5; i > 0; i--) { 17 System.out.print(i + " "); // Output: 5 4 3 2 1 18 } 19 } 20}

While Loop Examples

Java
1public class WhileLoopDemo { 2 public static void main(String[] args) { 3 int count = 0; 4 5 // Using postfix 6 while (count < 5) { 7 System.out.println("Count: " + count); 8 count++; // Increment at end of loop 9 } 10 // Output: Count: 0, 1, 2, 3, 4 11 12 // Using prefix (same result) 13 count = 0; 14 while (count < 5) { 15 System.out.println("Count: " + count); 16 ++count; // Increment at end of loop 17 } 18 } 19}

Difference in Loop Condition

Java
1public class LoopConditionDemo { 2 public static void main(String[] args) { 3 int i = 0; 4 5 // Using postfix in condition 6 while (i++ < 5) { 7 System.out.print(i + " "); // Output: 1 2 3 4 5 8 } 9 // i is compared (0<5, 1<5, ...), THEN incremented 10 11 System.out.println(); 12 i = 0; 13 14 // Using prefix in condition 15 while (++i < 5) { 16 System.out.print(i + " "); // Output: 1 2 3 4 17 } 18 // i is incremented, THEN compared (1<5, 2<5, ...) 19 } 20}

With Arrays

Array Traversal

Java
1public class ArrayTraversal { 2 public static void main(String[] args) { 3 int[] numbers = {10, 20, 30, 40, 50}; 4 5 // Using postfix (most common) 6 int i = 0; 7 while (i < numbers.length) { 8 System.out.println("numbers[" + i + "] = " + numbers[i]); 9 i++; 10 } 11 12 // Using array with increment in access 13 int[] data = {5, 10, 15, 20}; 14 int index = 0; 15 16 System.out.println(data[index++]); // Prints 5, then index becomes 1 17 System.out.println(data[index++]); // Prints 10, then index becomes 2 18 System.out.println(data[index++]); // Prints 15, then index becomes 3 19 } 20}

Array Index Manipulation

Java
1public class ArrayIndexDemo { 2 public static void main(String[] args) { 3 int[] arr = {100, 200, 300, 400, 500}; 4 int i = 0; 5 6 // Post-increment - use current, then increment 7 System.out.println(arr[i++]); // Prints arr[0]=100, then i=1 8 System.out.println(arr[i++]); // Prints arr[1]=200, then i=2 9 System.out.println(arr[i++]); // Prints arr[2]=300, then i=3 10 11 // i is now 3 12 13 // Pre-decrement - decrement, then use 14 System.out.println(arr[--i]); // i=2, prints arr[2]=300 15 System.out.println(arr[--i]); // i=1, prints arr[1]=200 16 } 17}

Common Mistakes

Mistake 1: Confusing Prefix and Postfix

Java
1public class CommonMistake1 { 2 public static void main(String[] args) { 3 int score = 0; 4 5 // ❌ WRONG - Expecting 1, but gets 0 6 int currentScore = score++; 7 System.out.println("currentScore: " + currentScore); // Output: 0 8 System.out.println("score: " + score); // Output: 1 9 10 // ✅ CORRECT - If you want the incremented value 11 score = 0; 12 currentScore = ++score; 13 System.out.println("currentScore: " + currentScore); // Output: 1 14 System.out.println("score: " + score); // Output: 1 15 } 16}

Mistake 2: Multiple Increments in One Statement

Java
1public class CommonMistake2 { 2 public static void main(String[] args) { 3 int x = 5; 4 5 // ❌ WRONG - Undefined behavior 6 // x = x++; // Don't do this! 7 8 // ✅ CORRECT - Use separately 9 x = x + 1; 10 // Or simply: 11 x++; 12 } 13}

Mistake 3: Using in Complex Expressions

Java
1public class CommonMistake3 { 2 public static void main(String[] args) { 3 int a = 5; 4 5 // ❌ CONFUSING - Hard to understand 6 int result = a++ + ++a + a++; 7 // Avoid this! Very hard to debug 8 9 // ✅ BETTER - Break into steps 10 a = 5; 11 int b = a; 12 a++; 13 int c = ++a; 14 int d = a++; 15 result = b + c + d; // Clear and maintainable 16 } 17}

Mistake 4: In Function Arguments

Java
1public class CommonMistake4 { 2 public static void printNumbers(int x, int y, int z) { 3 System.out.println(x + ", " + y + ", " + z); 4 } 5 6 public static void main(String[] args) { 7 int count = 1; 8 9 // ❌ CONFUSING - Order of evaluation unclear 10 // printNumbers(count++, count++, count++); 11 12 // ✅ BETTER - Increment separately 13 printNumbers(count, count + 1, count + 2); 14 count += 3; 15 } 16}

Mistake 5: Expecting Different Behavior Standalone

Java
1public class CommonMistake5 { 2 public static void main(String[] args) { 3 int x = 5; 4 5 // These are IDENTICAL when used standalone: 6 x++; // x becomes 6 7 8 x = 5; 9 ++x; // x becomes 6 10 11 // No difference! Only matters in expressions. 12 } 13}

Practical Examples

Example 1: Counter

Java
1public class Counter { 2 private int count; 3 4 public Counter() { 5 this.count = 0; 6 } 7 8 public int increment() { 9 return ++count; // Increment and return new value 10 } 11 12 public int decrement() { 13 return --count; // Decrement and return new value 14 } 15 16 public int getCount() { 17 return count; 18 } 19 20 public static void main(String[] args) { 21 Counter counter = new Counter(); 22 23 System.out.println("Increment: " + counter.increment()); // 1 24 System.out.println("Increment: " + counter.increment()); // 2 25 System.out.println("Increment: " + counter.increment()); // 3 26 System.out.println("Decrement: " + counter.decrement()); // 2 27 System.out.println("Count: " + counter.getCount()); // 2 28 } 29}

Example 2: Array Pointer Movement

Java
1public class ArrayPointer { 2 public static void main(String[] args) { 3 int[] data = {10, 20, 30, 40, 50}; 4 int pointer = 0; 5 6 // Move forward 7 System.out.println("Current: " + data[pointer++]); // 10, pointer=1 8 System.out.println("Current: " + data[pointer++]); // 20, pointer=2 9 System.out.println("Current: " + data[pointer++]); // 30, pointer=3 10 11 // Move backward 12 System.out.println("Current: " + data[--pointer]); // 30, pointer=2 13 System.out.println("Current: " + data[--pointer]); // 20, pointer=1 14 } 15}

Example 3: ID Generator

Java
1public class IDGenerator { 2 private static int nextID = 1000; 3 4 public static int generateID() { 5 return nextID++; // Return current, then increment 6 } 7 8 public static void main(String[] args) { 9 System.out.println("User ID: " + generateID()); // 1000 10 System.out.println("User ID: " + generateID()); // 1001 11 System.out.println("User ID: " + generateID()); // 1002 12 System.out.println("Next ID will be: " + nextID); // 1003 13 } 14}

Example 4: Stack Implementation (Simplified)

Java
1public class SimpleStack { 2 private int[] data; 3 private int top; 4 5 public SimpleStack(int size) { 6 data = new int[size]; 7 top = -1; // Empty stack 8 } 9 10 public void push(int value) { 11 if (top < data.length - 1) { 12 data[++top] = value; // Increment top, then store 13 } 14 } 15 16 public int pop() { 17 if (top >= 0) { 18 return data[top--]; // Return current, then decrement 19 } 20 return -1; // Empty 21 } 22 23 public static void main(String[] args) { 24 SimpleStack stack = new SimpleStack(5); 25 26 stack.push(10); 27 stack.push(20); 28 stack.push(30); 29 30 System.out.println("Pop: " + stack.pop()); // 30 31 System.out.println("Pop: " + stack.pop()); // 20 32 System.out.println("Pop: " + stack.pop()); // 10 33 } 34}

Best Practices

Increment/Decrement Best Practices:

1. Prefer Standalone Usage:

Java
1// ✅ GOOD - Clear and simple 2count++; 3total--;

2. Avoid in Complex Expressions:

Java
1// ❌ BAD - Hard to understand 2result = x++ + y++ * z--; 3 4// ✅ GOOD - Break into steps 5result = x + y * z; 6x++; 7y++; 8z--;

3. In Loops - Postfix is Standard:

Java
1// ✅ Standard convention 2for (int i = 0; i < n; i++) { 3 // ... 4}

4. When You Need the Value - Choose Carefully:

Java
1// Need old value? Use postfix 2int oldValue = count++; 3 4// Need new value? Use prefix 5int newValue = ++count;

5. Never Modify Same Variable Twice:

Java
1// ❌ WRONG - Undefined behavior 2x = x++; 3result = ++i + i++; 4 5// ✅ CORRECT - Separate statements 6x++; 7result = i + j;

6. Be Explicit for Clarity:

Java
1// When in doubt, be explicit: 2count = count + 1; // Always clear 3// Instead of: 4count++; // Only if the context is simple

Remember:

  • Prefix: ++x → Change first, use new value
  • Postfix: x++ → Use current value, change after
  • Standalone: No difference between prefix and postfix
  • In expressions: Big difference!
  • Keep it simple: Avoid multiple operators in one statement

Quick Reference

Behavior Summary

OperatorNameActionReturns
++xPre-incrementAdd 1 firstNew value
x++Post-incrementAdd 1 afterOld value
--xPre-decrementSubtract 1 firstNew value
x--Post-decrementSubtract 1 afterOld value

When to Use Which

ScenarioRecommendedWhy
Standalone statementEitherNo difference
For loop incrementi++Convention
Need return valueChoose based on needPrefix for new, postfix for old
Array index movementarr[i++]Get current, move forward
Counter that returnsreturn ++countReturn updated value
ID generationreturn id++Return current, increment for next

What's Next?

Now that you understand increment and decrement operators:

  1. Arithmetic Operators - Other math operators
  2. Assignment Operators - +=, -=, etc.
  3. Loops - Using operators in loops

Congratulations! You now understand the subtle but important differences between prefix and postfix increment/decrement operators!

Increment & Decrement Operators | DevStackFlow