Post-Increment Operator
Whenever we write the ++ sign after a variable or an operator, it is known as a post-increment operator. The value of the operand is first used for calculation or in the expression; after that only, it is increased by one.
Syntax
<operand>++
Example
i++, where i is a variable
Implementation
Java
public class PostIncrementExample {
public static void main(String[] args) {
int i = 10;
System.out.println("Value of i before post-incrementing: " + i);
// post-increment
int a = i++;
System.out.println("Value of a: " + a);
// value after post-incrementing
System.out.println("Value of i after post-incrementing: " + i);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Value of i before post-incrementing: 10
Value of a: 10
Value of i after post-incrementing: 11
Explanation
In this code, the variable i is initialized to 10. The post-increment operator (i++) uses the current value of i (10) in the assignment to a before incrementing i by 1. As a result, a holds 10, and i becomes 11 after the operation. The output demonstrates how post-increment first uses the value and then increments it.
Special Case for Post-Increment Operator
Do you wonder what would happen if we assign the post-incremented value of a variable to the same variable?
The answer is variable's value stays constant. This is because the post-increment operation is performed after the assignment operation of the same variable. i.e the value of the variable is incremented after it is assigned in the same variable.
Example
Java
public class IncrementExample {
public static void main(String[] args) {
int i = 10;
System.out.println("Value of i before increment: " + i);
i = i++;
System.out.println("Value of i after increment: " + i);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Value of i before increment: 10
Value of i after increment: 10
Explanation
In this code, the variable i is initialized to 10. The statement i = i++ uses the current value of i (10) to assign back to i before the increment takes effect. As a result, the increment is effectively discarded, and i remains 10. This demonstrates how post-increment behaves when combined with assignment to the same variable.
Readability
Both i++ and ++i are increment operators in Java, but they differ in their behavior and readability. i++ is the postfix increment operator, which increments the value of i after it is used in an expression. On the other hand, ++i is the prefix increment operator, which increments the value of i before it is used in an expression.
The Increment Step
- i++: Postfix increment operator. The current value of i is used in the expression, and then i is incremented.
- ++i: Prefix increment operator. i is incremented first, and then the updated value of i is used in the expression.
Frequently Asked Questions
What is a unary operator in Java?
An operator with only one operand is a unary operator in Java.
What is the difference between pre-increment and post-increment?
In a pre-increment process, the operand's value is first raised by one before being used for any other purpose. In contrast, in a post-increment process, the operand's value is first used before being raised by one.
Is pre or post increment faster?
Pre-increment is often faster because it increments and returns the new value directly. Post-increment, however, creates a temporary copy of the original value before incrementing. The performance difference is typically negligible with modern compilers.
What is the use of the operators in Java?
An operator is used to alter certain data elements and output a result. These data elements are referred to as operands or arguments. The operator is applied to the operand to make the required changes in the operand.
Conclusion
This article discusses the difference between pre-increment (++i) and post-increment (i++) operators. We hope this blog has helped you enhance your knowledge of increment operators. If you want to learn more, then check out our articles.