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

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

In Java, the increment operator (++) is used to increase the value of a variable by one. There are two types of increment operators in Java: pre-increment (++i) and post-increment (i++), and these two operators perform differently.

In this blog, we will learn the difference between post-increment (i++) and pre-increment(++i) in Java with the help of some examples.

Table of Contents:

What is Increment Operator in Java?

The increment operator (++) in Java is a unary operator that increases the value of a variable by one. It is commonly used in loops and expressions where a variable needs to be updated iteratively.

Java provides two types of increment operators:

  • Pre-increment (++i): It increments the value first, then uses it in the expression.
  • Post-increment (i++): It uses the current value in the expression first, then increments it.

You can use these operators in scenarios where you need counter-variables and iterative operations.

Pre-Increment Operator (++i) in Java

The pre-increment operator (++i) increases the value of a variable before using it in an expression. This means the variable is incremented first, and then its updated value is used in the operation. This operator is commonly used when the updated value needs to be used immediately in an expression.

Syntax:

++variable;

Example:

Java

Output:

Pre-Increment Operator (++i) in Java Output

Explanation:

  • The value of num is incremented first (num becomes 6).
  • The updated value (6) is then assigned to the result.

Post-Increment Operator (i++) in Java

The post-increment operator (i++) increases the value of a variable after using it in an expression. This means the current value is used first, and then the variable is incremented. This operator is useful when you need to use the original value in an expression before increasing it.

Syntax:

variable++;

Example:

Java

Output:

Post-Increment Operator (i++) in Java

Explanation:

  • The current value of num (5) is first assigned to the result.
  • Then, num is incremented to 6.

Key Difference Between Pre Increment(++i) and Post Increment(i++) In Java

Here are the following differences between pre-increment and post-increment in Java:

Feature Pre-Increment (++i)Post-Increment (i++)
When increment happensBefore the variable is used in the expressionAfter the variable is used in the expression
The value used in the expressionThe updated (incremented) value is usedThe original (old) value is used
Effect on variableThe variable increases before any operationThe variable increases after any operation
Usage in loopsOften used when the incremented value is needed immediatelyOften used when the current value is needed before incrementing
PerformanceSlightly faster in some cases (avoids temporary storage of old value)Slightly slower in expressions as it may require temporary storage
Example in Assignment (b = ++a;)a is incremented first, then assigned to ba is assigned to b first, then incremented
Common Use CasesUsed when an immediate incremented value is required in calculationsUsed when the original value is required before updating

Practical Examples of Pre Increment(++i) and Post Increment(i++) In Java

Let’s learn some practical examples to understand the difference between pre-increment (++i) and post-increment (i++):

Example 1: Usage in Loops

We can use ++ (increment) operators inside a loop to increase the loop counter.

Pre-Increment in a Loop: Let’s use a pre-increment operator inside a loop:

Java

Output:

Usage in Loops Output

Post-Increment in a Loop: Let’s use the post-increment operator inside a loop:

Java

Output:

Post-Increment in a Loop Output

Explanation:

  • Both ++i (pre-increment) and i++ (post-increment) work the same way in a loop because the increment happens at the end of each iteration.
  • However, pre-increment (++i) can be slightly faster since it directly increases the value without keeping a copy of the old one.

Example 2: Testing the Condition inside a loop

We can also use pre-increment (++i) and post-increment (i++) to test the condition inside a loop:

Testing the Condition With the Pre-Increment: Let’s check the condition of a loop with pre-increment in Java:

Example:

Java

Output:

Testing the Condition inside a loop Output

Explanation: Loop Condition: while (++i <= 5)

  • The pre-increment (++i) increases i before checking the condition.
  • The loop runs as long as i ≤ 5.

Loop Execution:

  • i starts at 0, but ++i increments it before checking.
  • Values printed: 1, 2, 3, 4, 5.
  • When i becomes 6, the condition fails, and the loop stops.

Testing the Condition With the Post-Increment: Let’s check the condition of a loop with post-increment in Java:

Example:

Java

Output:

Testing the Condition With the Post-Increment Output

Explanation: The condition i++ <= 5 means:

  • First, i is compared with 5.
  • Then, i is incremented after the comparison.
  • Since post-increment (i++) returns the value before incrementing, when i == 5, it still allows execution of the loop once more.
  • This results in i being printed from 1 to 6.

Example 3: Usage in Expressions

Let’s use pre-increment (++i) and post-increment (i++) inside the expressions:

Java

Output:

Usage in Expressions Output

Explanation:

1. Post-increment (x++ * 2)

  • Uses x = 3 first, then increments it to 4.
  • Output: 3 * 2 = 6

2. Pre-increment (++x * 2)

  • Increments x from 4 to 5, then use 5 in the expression.
  • Output: 5 * 2 = 10

Error on a Constant Value by using Java Increment Operators

You can not use pre-increment (++i) and post-increment (i++) on a constant value, it will give you an error.

Example:

Java

Output:

Error on a Constant Value by using Java Increment Operators Output

Explanation: It is not allowed to use pre-increment and post-increment operators on constant values.

Conclusion

In this blog, we have learned the difference between pre-increment (++i) and post-increment (i++) in Java. We have explored how they work, their usage in expressions, and when to use them. We have also looked at practical examples to understand their behavior in more detail.

If you want to learn more about Java, you may refer to our Java Course.

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner