Table of contents
1.
Introduction
2.
Pre-Increment Operator
2.1.
Syntax
2.2.
Example 
2.3.
Implementation
2.4.
Java
3.
Post-Increment Operator
3.1.
Syntax
3.2.
Example
3.3.
Implementation
3.4.
Java
4.
Special Case for Post-Increment Operator
4.1.
Example
4.2.
Java
5.
Readability
6.
The Increment Step
7.
Frequently Asked Questions
7.1.
What is a unary operator in Java?
7.2.
What is the difference between pre-increment and post-increment?
7.3.
Is pre or post increment faster?
7.4.
What is the use of the operators in Java?
8.
Conclusion
Last Updated: Jan 22, 2025
Easy

What is the Difference Between i++ and ++i in Java?

Author Sohail Ali
2 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Increment operators are the unary operators used to raise the value of an operand by unity. This operator works solely with variables only. The ++ symbol denotes the increment operator. Depending on how a value is increased, increment operators are of two types, i.e., pre-increment (++<operand>) and post-increment (<operand>++) operators.

post increment and pre increment in java

This blog will discuss the increment operator and the difference between i++ and ++i operators in Java.

Pre-Increment Operator

Whenever we write the ++ sign before a variable or an operator, it is known as a pre-increment operator. In a pre-increment process, the operand's value is first raised by one before being used for any further calculation.

Syntax

++<operand>

Example 

++i, where i is a variable.

Implementation

  • Java

Java

public class PreIncrementExample {
public static void main(String[] args) {
int i = 10;
System.out.println("Value of a before pre-incrementing: " + i);
// pre-increment
int a= ++i;
System.out.println("Value of a: " + a);
// value after pre-incrementing
System.out.println("Value of i after pre-incrementing: " + i);
}
}
You can also try this code with Online Java Compiler
Run Code

 

Output

Value of a before pre-incrementing: 10
Value of a: 11
Value of i after pre-incrementing: 11

 

Explanation 

In this code, the variable i is initialized to 10. The pre-increment operator (++i) increments i by 1 before its value is assigned to a. As a result, both i and a have the value 11 after the operation. The output demonstrates how pre-increment modifies the variable before it's used in an expression.

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

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

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.

Live masterclass