DEV Community

Cover image for Increment and Decrement Operators in Java
Rajesh Bhola
Rajesh Bhola

Posted on

Increment and Decrement Operators in Java

Increment (++) and decrement (--) operators are among the most commonly used operators in Java.

They allow you to increase or decrease the value of a variable by 1 with a single operator.

Although they look simple, understanding how they work—especially the difference between prefix and postfix forms—is essential for writing correct Java programs and performing well in interviews.

Let's learn them with simple explanations and practical examples.


What Are Increment and Decrement Operators?

Java provides two unary operators:

  • Increment (++) increases a variable's value by 1.
  • Decrement (--) decreases a variable's value by 1.

Example

int count = 10;

count++;

System.out.println(count);
Enter fullscreen mode Exit fullscreen mode

Output

11
Enter fullscreen mode Exit fullscreen mode

Similarly,

count--;
Enter fullscreen mode Exit fullscreen mode

reduces the value by one.


Types of Increment Operator

The increment operator has two forms.

  • Prefix Increment (++x)
  • Postfix Increment (x++)

Although both increase the value by 1, the order of execution is different.


Prefix Increment (++x)

The variable is incremented first, and then its updated value is used.

Example

int score = 10;

int updatedScore = ++score;

System.out.println(score);
System.out.println(updatedScore);
Enter fullscreen mode Exit fullscreen mode

Output

11
11
Enter fullscreen mode Exit fullscreen mode

What happened?

  1. score became 11.
  2. The updated value (11) was assigned to updatedScore.

Postfix Increment (x++)

The current value is used first, and then the variable is incremented.

Example

int score = 10;

int previousScore = score++;

System.out.println(score);
System.out.println(previousScore);
Enter fullscreen mode Exit fullscreen mode

Output

11
10
Enter fullscreen mode Exit fullscreen mode

What happened?

  1. The current value (10) was assigned to previousScore.
  2. After the assignment, score became 11.

Prefix Decrement (--x)

The variable is decreased first, and then the updated value is used.

Example

int items = 5;

int remainingItems = --items;

System.out.println(items);
System.out.println(remainingItems);
Enter fullscreen mode Exit fullscreen mode

Output

4
4
Enter fullscreen mode Exit fullscreen mode

Postfix Decrement (x--)

The current value is used first, and then it is decreased.

Example

int items = 5;

int previousItems = items--;

System.out.println(items);
System.out.println(previousItems);
Enter fullscreen mode Exit fullscreen mode

Output

4
5
Enter fullscreen mode Exit fullscreen mode

Prefix vs Postfix

Assume the initial value is:

int x = 10;
Enter fullscreen mode Exit fullscreen mode

Prefix Increment

int y = ++x;
Enter fullscreen mode Exit fullscreen mode

Result

x = 11
y = 11
Enter fullscreen mode Exit fullscreen mode

Postfix Increment

int y = x++;
Enter fullscreen mode Exit fullscreen mode

Result

x = 11
y = 10
Enter fullscreen mode Exit fullscreen mode

Prefix Decrement

int y = --x;
Enter fullscreen mode Exit fullscreen mode

Result

x = 9
y = 9
Enter fullscreen mode Exit fullscreen mode

Postfix Decrement

int y = x--;
Enter fullscreen mode Exit fullscreen mode

Result

x = 9
y = 10
Enter fullscreen mode Exit fullscreen mode

Rule 1: Increment and Decrement Work Only on Variables

Valid

int number = 5;

number++;
Enter fullscreen mode Exit fullscreen mode

Invalid

++5;
Enter fullscreen mode Exit fullscreen mode

Compile-time error

unexpected type
required: variable
found: value
Enter fullscreen mode Exit fullscreen mode

Why?

The ++ operator changes a value.

Only variables can store a new value.

A constant like 5 cannot be modified.


Rule 2: Nested Increment Is Not Allowed

Invalid

int value = 5;

++(++value);
Enter fullscreen mode Exit fullscreen mode

Compile-time error

unexpected type
required: variable
found: value
Enter fullscreen mode Exit fullscreen mode

Why?

The result of ++value is a value, not another variable.


Rule 3: Cannot Be Used with final Variables

final int MAX = 100;

MAX++;
Enter fullscreen mode Exit fullscreen mode

Compile-time error

cannot assign a value to final variable
Enter fullscreen mode Exit fullscreen mode

A final variable cannot be modified after initialization.


Rule 4: Cannot Be Applied to boolean

Valid

int number = 10;

number++;
Enter fullscreen mode Exit fullscreen mode
char grade = 'A';

grade++;
Enter fullscreen mode Exit fullscreen mode
double price = 99.99;

price++;
Enter fullscreen mode Exit fullscreen mode

Invalid

boolean active = true;

active++;
Enter fullscreen mode Exit fullscreen mode

Compile-time error

bad operand type boolean for unary operator '++'
Enter fullscreen mode Exit fullscreen mode

Java allows increment and decrement for every primitive type except boolean.


Incrementing Characters

Characters are internally represented using Unicode values.

Example

char grade = 'A';

grade++;

System.out.println(grade);
Enter fullscreen mode Exit fullscreen mode

Output

B
Enter fullscreen mode Exit fullscreen mode

Similarly,

char letter = 'z';

letter--;

System.out.println(letter);
Enter fullscreen mode Exit fullscreen mode

Output

y
Enter fullscreen mode Exit fullscreen mode

Why Does byte++ Work but b = b + 1 Doesn't?

This is one of the most popular Java interview questions.

Consider this code.

byte value = 10;

value = value + 1;
Enter fullscreen mode Exit fullscreen mode

Compile-time error

possible lossy conversion from int to byte
Enter fullscreen mode Exit fullscreen mode

Why?

In Java, arithmetic operations on byte, short, and char are automatically promoted to int.

So,

value + 1
Enter fullscreen mode Exit fullscreen mode

produces an int.

Assigning an int back to a byte requires an explicit cast.

Correct version

byte value = 10;

value = (byte)(value + 1);
Enter fullscreen mode Exit fullscreen mode

Now look at this.

byte value = 10;

value++;

System.out.println(value);
Enter fullscreen mode Exit fullscreen mode

Output

11
Enter fullscreen mode Exit fullscreen mode

Why does this work?

The compiler automatically performs the required cast internally.

Conceptually, Java treats it like this:

value = (byte)(value + 1);
Enter fullscreen mode Exit fullscreen mode

This automatic conversion is built into the increment and decrement operators.


A Common Interview Question

Consider the following program.

int x = 10;

x = x++;

System.out.println(x);
Enter fullscreen mode Exit fullscreen mode

What will be the output?

Output

10
Enter fullscreen mode Exit fullscreen mode

Step-by-step explanation

Initially,

x = 10
Enter fullscreen mode Exit fullscreen mode

The postfix increment works as follows:

  1. Save the current value (10) for the assignment.
  2. Increment x to 11.
  3. Assign the saved value (10) back to x.

Final result

x = 10
Enter fullscreen mode Exit fullscreen mode

This surprises many beginners because they expect the answer to be 11.


Common Beginner Mistakes

Forgetting the Difference Between Prefix and Postfix

int x = 5;

System.out.println(++x);
Enter fullscreen mode Exit fullscreen mode

Output

6
Enter fullscreen mode Exit fullscreen mode

Whereas

int x = 5;

System.out.println(x++);
Enter fullscreen mode Exit fullscreen mode

Output

5
Enter fullscreen mode Exit fullscreen mode

After printing,

x = 6
Enter fullscreen mode Exit fullscreen mode

Trying to Increment a Constant

Invalid

++100;
Enter fullscreen mode Exit fullscreen mode

Only variables can be incremented.

Using Multiple Increment Operators in One Expression

Avoid code like this.

int i = 1;

int result = ++i + i++;
Enter fullscreen mode Exit fullscreen mode

Although Java defines the evaluation order, such expressions are difficult to read and maintain.

Professional Java developers prefer simple, readable code.

Instead, write

i++;

int result = i;

i++;
Enter fullscreen mode Exit fullscreen mode

This is much easier to understand and debug.


Best Practices

  • Use increment and decrement only on variables.
  • Prefer writing one increment or decrement operation per statement.
  • Avoid modifying the same variable multiple times in a single expression.
  • Use meaningful variable names instead of x, i, or a in business applications.
  • Focus on writing readable code rather than clever one-line expressions.

Key Takeaways

  • ++ increases a variable by 1, while -- decreases it by 1.
  • Prefix operators (++x, --x) modify the variable before using its value.
  • Postfix operators (x++, x--) use the current value first and then modify it.
  • Increment and decrement operators work only with variables, not constants.
  • They cannot be applied to boolean values.
  • They cannot be used with final variables.
  • Java automatically handles type conversion for ++ and --, which is why byte++ works even though byte = byte + 1 does not.
  • Avoid complex expressions that modify the same variable multiple times. Clear and readable code is always a better choice.

Happy Coding!

Top comments (0)