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);
Output
11
Similarly,
count--;
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);
Output
11
11
What happened?
-
scorebecame 11. - 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);
Output
11
10
What happened?
- The current value (10) was assigned to
previousScore. - After the assignment,
scorebecame 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);
Output
4
4
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);
Output
4
5
Prefix vs Postfix
Assume the initial value is:
int x = 10;
Prefix Increment
int y = ++x;
Result
x = 11
y = 11
Postfix Increment
int y = x++;
Result
x = 11
y = 10
Prefix Decrement
int y = --x;
Result
x = 9
y = 9
Postfix Decrement
int y = x--;
Result
x = 9
y = 10
Rule 1: Increment and Decrement Work Only on Variables
Valid
int number = 5;
number++;
Invalid
++5;
Compile-time error
unexpected type
required: variable
found: value
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);
Compile-time error
unexpected type
required: variable
found: value
Why?
The result of ++value is a value, not another variable.
Rule 3: Cannot Be Used with final Variables
final int MAX = 100;
MAX++;
Compile-time error
cannot assign a value to final variable
A final variable cannot be modified after initialization.
Rule 4: Cannot Be Applied to boolean
Valid
int number = 10;
number++;
char grade = 'A';
grade++;
double price = 99.99;
price++;
Invalid
boolean active = true;
active++;
Compile-time error
bad operand type boolean for unary operator '++'
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);
Output
B
Similarly,
char letter = 'z';
letter--;
System.out.println(letter);
Output
y
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;
Compile-time error
possible lossy conversion from int to byte
Why?
In Java, arithmetic operations on byte, short, and char are automatically promoted to int.
So,
value + 1
produces an int.
Assigning an int back to a byte requires an explicit cast.
Correct version
byte value = 10;
value = (byte)(value + 1);
Now look at this.
byte value = 10;
value++;
System.out.println(value);
Output
11
Why does this work?
The compiler automatically performs the required cast internally.
Conceptually, Java treats it like this:
value = (byte)(value + 1);
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);
What will be the output?
Output
10
Step-by-step explanation
Initially,
x = 10
The postfix increment works as follows:
- Save the current value (
10) for the assignment. - Increment
xto11. - Assign the saved value (
10) back tox.
Final result
x = 10
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);
Output
6
Whereas
int x = 5;
System.out.println(x++);
Output
5
After printing,
x = 6
Trying to Increment a Constant
Invalid
++100;
Only variables can be incremented.
Using Multiple Increment Operators in One Expression
Avoid code like this.
int i = 1;
int result = ++i + i++;
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++;
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, orain 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
booleanvalues. - They cannot be used with
finalvariables. - Java automatically handles type conversion for
++and--, which is whybyte++works even thoughbyte = byte + 1does 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)