Increment & Decrement Operators in Java

πŸ” What Are They?

These operators are used to increase or decrease a variable's value by 1.

πŸ”Ή Increment Operator (++)

  • Increases the value by 1

πŸ”Έ Decrement Operator (--)

  • Decreases the value by 1


βœ… Two Forms: Pre and Post

Type Example What it does
Pre-increment ++x Increments first, then uses the value
Post-increment x++ Uses the value first, then increments it
Pre-decrement --x Decrements first, then uses the value
Post-decrement x-- Uses the value first, then decrements it

πŸ” Example:

int x = 5;

System.out.println(++x); // 6 (pre-increment)
System.out.println(x++); // 6 (prints first, then becomes 7)
System.out.println(x); Β  // 7

System.out.println(--x); // 6 (pre-decrement)
System.out.println(x--); // 6 (prints first, then becomes 5)
System.out.println(x); Β  // 5


⚠️ Common Use-Cases:

  • Loop counters (for, while)
  • Simple arithmetic
  • Conditional updates

🧠 Tip:

  • Use pre (++x) when you need the updated value immediately.
  • Use post (x++) when the update can wait until after the current use.

Β 

Β 

Increment and Decrement Operators | Casting Primitive Values | Java  Sertifikat QeydlΙ™rim

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.