Understanding Java Increment and Decrement Operators
1. Increment and Decrement: The Basics

The ++ and -- operators perform increment and decrement. The increment operation increases the operand value by one, while the decrement operation decreases it by one. For example, the expression:
x = x + 1; can be rewritten using the increment operator as:
x++; Similarly, the following expression:
x = x - 1; is equivalent to:
x--; 2. Prefix and Postfix Forms
These operations can be written in postfix form, where the operation follows the operand, or in prefix form, where the operation precedes the operand.
Let’s examine the difference between prefix and postfix increment in Java using the following example. The difference appears only when the increment is used in another operation.
For instance, in the statement int b = a++;, the value of a is first assigned to b, and only then does the increment take place (postfix form means "after"). As a result, the value of b will be 1. Now, consider the next statement - int c = ++a; - here, the increment happens first (prefix form means "before"), and only then is the new value of a assigned to c.
When the increment is not part of another operation, like in the statement c++, it does not matter whether you use the prefix or postfix form.
public class IncrementDecrement {
public static void main(String[] args) {
int a = 1;
int b = a++;
int c = ++a;
c++;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
}
} Program output:
a = 3
b = 1
c = 4 3. Decrement: Prefix and Postfix
The decrement operator works exactly the same way. The difference between the prefix and postfix forms only shows up when the operation is part of another expression. Here's a similar example using --:
public class DecrementExample {
public static void main(String[] args) {
int a = 5;
int b = a--; // postfix: b = 5 first, then a becomes 4
int c = --a; // prefix: a becomes 3 first, then c = 3
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
}
} Program output:
a = 3
b = 5
c = 3 Just like with the increment: in the postfix form a-- the current value is used first and only then decreased; in the prefix form --a the value is decreased first and then used.
4. Increment on the char Type
The increment operation can also be applied to variables of type char. In this case, the increment is performed on the character's Unicode code. Since characters are ordered alphabetically in the Unicode table, applying the increment operator to a character value results in the next character in alphabetical order:
public class CharInc {
public static void main(String[] args) {
char ch = 'X';
System.out.println("ch contains " + ch);
ch++; // incrementing ch
System.out.println("New value of ch: " + ch);
}
} Program output:
ch contains X
New value of ch: Y Frequently Asked Questions
What's the difference between prefix (++a) and postfix (a++) increment?
Both increase the variable by one — the difference is only when it happens relative to using the value. The prefix form ++a increments the variable first, then returns the new value. The postfix form a++ returns the current value first, then increments the variable. The same rule applies to the decrement forms --a and a--.
When does the difference between prefix and postfix matter?
The difference only shows up when the operation is part of another expression — for example, in an assignment like int b = a++; or inside a method call. If the increment is on its own line, like a++; or ++a;, the result is the same and the form doesn't matter.
What is the output of int a = 1; int b = a++;?
b is assigned the value 1, and a becomes 2. This is the postfix form: the current value of a (which is 1) is assigned to b first, and only then is a increased by one. If the prefix form int b = ++a; were used, both a and b would become 2.
Can the increment operator be applied to a char variable?
Yes. The increment is applied to the character's Unicode code. Since characters are ordered in Unicode, incrementing gives the next character: for example, for char ch = 'X';, after ch++ the variable contains 'Y'. Decrement works the same way, returning the previous character.
Comments