Java

What is difference between i++ and ++i in java

Java offers a wide range of operators that assist us to perform various operations on variables/values, for instance, unary operators, arithmetic operators, logical operators, etc. The ++ is an increment operator that belongs to the unary operator’s category and can be used as a prefix or postfix. Sometimes it is also referred to as a pre-increment and post-increment operator. It is most frequently used in java loops.This post will give you a comprehensive guide to the below-listed concepts:

So let’s start!

What does ++ Mean in Java

The ++ sign is a unary operator commonly known as the java increment operator. It increases the variable’s value by 1. In java, the increment operator can be utilized as either Prefix or Postfix based on the situation/requirements.

What is Prefix Operator in Java

Pre-increment, as the name itself suggests the ++ sign will appear before the variable i.e. “++i” where “i” is a variable. The prefix operator will first increment the variable’s value by one and afterward, it will return the incremented value.

What is Postfix Operator in Java

In Postfix increment, the ++ operator appears after the variable, i.e., “i ++”. The post-increment operator first, returns the variable’s original value, and afterward, increments the variable’s value by 1.

How Pre-increment Operator Works in Java

Let’s consider a couple of examples to understand how a pre-increment operator works in java.

Example 1

In this example, we will create a variable and will increase its value using pre-increment operator:

public class IncrementExample {

public static void main(String[] args) {

int i = 14;

System.out.println("Pre Increment: " + (++i));

}

}

The snippet given below will show the output:

The output verified the working of the java pre-increment operator.

How Post-increment Operator Works in Java

Let’s consider the example given below, for a better understanding of the postfix increment operator in java.

Example 2

Let’s modify the previous example a bit more and apply the post-increment operator instead of the pre-increment operator:

public class PrefixPostfixExample {

public static void main(String[] args) {

int i = 14;

System.out.println("Result: " + (i++));

}

}

Now we will get the following output:

This time the output showed the actual(non-incremented) value because the post-increment first returned the actual value(14) and then incremented the original value by one(15). But how to verify it?

Printing the variable “i” one more time will provide the incremented value. Let’s try:

public class IncrementExample {

public static void main(String[] args) {

int num1 = 14;

System.out.println("Result: " + (i++));

System.out.println("Result: " + (i));

}

}

The snippet given below will provide the desired output:

The output verified the appropriateness of post-increment operator.

Conclusion

The ++ sign will appear before the variable in the pre-increment operator, i.e., “++i,” where “i” is a variable. The prefix operator will first increment the variable’s value by one, and afterward, it will return the incremented value. While in Postfix increment, the ++ operator appears after the variable, i.e., “i ++”. The post-increment operator first returns the variable’s original value and, afterward, increments the variable’s value by one. This write-up elaborated on the difference between prefix and postfix increment with the help of some easy-to-understand examples.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.