Difference between I ++ and ++ I operator

Difference between i ++ and ++ i operators in Java

  1. How-To's
  2. Java Howtos
  3. Difference between i ++ and ++ i operators in Java

Created: March-30, 2021 | Updated: April-06, 2021

  1. Operator Pre-Increment () in Java
  2. Operator Pre-Increment () vs Post-Increment () in Java

This tutorial introduces differences between the before and after the increment operators in Java.

In Java, the and operators are called increment operators. The operator is called the pre-increment operator, while the operator is called the post-increment operator. As the name suggests, the after incrementing operator increases the variable after it is used, and the before incrementing operator increases the variable before it is used. These are also unary operators.

There are several ways to use these operators, such as: B. in the loop for incrementing the loop condition variable to iterate all elements of a in Java. For example the loop, the loop, the list or stream method, and so on. Let's look at some examples.

Operator Pre-Increment () in Java

The increment operators are mostly used in a loop to automate the loop iterations. In this example, we'll use the pre-increment operator to increment the variable by 1 each iteration of the loop. This is a simple example that doesn't explain the correct difference between both increment operators, but we can get an idea of ​​how to use this in the loop. See the example below.

Output:

Operator Pre-Increment () vs Post-Increment () in Java

In this example we can clearly see the difference between operators before and after the increment. We take a variable and apply it after incrementing and see that it prints the same value that it contains as it is incremented after one use. And we create a variable that prints the incremented value because it is incremented before use. See the example below.

Output:

Related Article - Java Operator

  • Question mark and colon in Java