Java offers a number of operators that are applied when needed depending upon the situation/scenario. The operators that Java deals with are conditional, arithmetic, unary, logical, etc. The increment operator belongs to the unary operator and is mostly implemented in for loops.
In this article, we will first understand the increment operator in Java and then move towards the difference between ++i (pre-increment) and i++ (post-increment).
What Does ++ Operator Do in Java Programming?
The “ increment (++)” operator in Java adds a value of “1” to the specified variable. It is usually used in loops like for loop and when there is a need to print a sequence of values. For instance, if we need to add the numbers from 1 to 10, the increment operator along with the for loop can be used to iterate and add the integers from 1 to 10.
Difference Between i++ and ++i in Java
There are two ways of declaring the increment operator as prefix increment and postfix increment. The “++” operator before the specified variable is termed “Pre-Increment” and the “++” operator after the variable is known as “Post-Increment”. The main difference between the two is that in the prefix increment of the variable, the value of the variable is incremented by 1 and the incremented value is returned whereas, in the postfix increment the original value is returned first and then the variable is incremented by 1.
Pre-Increment (++i) in Java
The pre-increment in Java increments the value of the declared variable by 1 and prints the result.
//Import the package
import java.io.*;
//Declare the class
class Example1 {
public static void main(String[] args)
{
// Initialized as per the need
int x = 10;
System.out.println("The value obtained by applying Pre-Increment is:");
System.out.println(++x);
}
}
In the above code block:
- The required “java.io” package is imported.
- In the next step, a class is declared as “Example1”.
- The variable declared as “x” is initialized with a value of “10”.
- The output of the value through the pre-increment operator is printed accordingly.
Output
In the below output the incremented value of “10” is printed as “11”.
Post-Increment (i++) in Java
The post-increment in Java prints the original value in the first step and then it increments the value. The code below shows the implementation.
//Import the package
import java.io.*;
//Declare the class
class Example1 {
public static void main(String[] args)
{
// initialized as per the need
int x = 8;
System.out.println("The value obtained by applying Post-Increment is: ");
System.out.println(x++);
}
}
In the above code, the value in the variable “x” is declared as “8” and the output is printed using the println() method of Java.
Output
The output is printed as the original value which is “8” since the value is incremented afterwards.
Verification of Post-Increment(i++) in Java
Since the output is exactly the same as the input provided, let us verify whether the code is working properly or not.
//Import the package
import java.io.*;
//Declare the class
class Example1 {
public static void main(String[] args)
{
// initialized as per the need
int x = 8;
System.out.println("The value obtained by applying Post-Increment is: " + x++);
System.out.println("The value obtained by applying Post-Increment again is: " + x);
}
}
In the code above the value is printed again, the next to observe whether the increment value is returned or not.
Output
The output below shows that for the first time “8” is printed and after the application of the print() function, for the second time the value appears to be “9”.
The implementation on the variable is done but what will be our output when either pre-increment or post-increment is applied on a constant value? Let us implement and observe the results.
Error on a Constant Value
//Import the required package
import java.io.*;
//Declare the class
class JavaExample3 {
public static void main(String[] args)
{
//Apply pre-increment on a constant value
int a = ++10;
System.out.println(a);
//Apply post-increment on a constant value
int b = 10++;
System.out.println(a);
}
}
In the above code block, the pre-increment and post-increment when applied on constant terms will eventually result in an error since it is only limited to variables.
Output
The output below shows that the pre-increment operator on a constant value of “10” throws an error that says “a variable is required instead of a value”, as depicted below.
The output below shows that the post-increment operator on a constant value of “10” throws an error. Since it requires variables and the operator is applied on a constant value that is depicted below.
This article further advances the implementation of the increment operator in the for loop.
Implementation of “for” Loop
The for loop in Java takes three parameters that are explained below
for (init_expression; loop_condition; increment/decrement) {
// code to proceed
}
In the syntax above:
- The “init_expression” executes itself only once before the main code.
- The “loop_condition” shows the condition used.
- The last part is “increment/decrement” which changes the value as the code block in the loop gets executed.
Let us see an example of implementing a for loop showing the increment operator.
class Example5{
public static void main(String[] args) {
System.out.println("The for Loop to Show Increment in Java");
//Initialize the Sum and the number
int a = 0;
int b = 25;
//for Loop to declare the integer values and print the sum
for (int y = 1; y <= b; ++y) {
a= a+y;
}
System.out.println("The Sum is = " + a);
}
}
In the above code, the integer values from 0 to 25 are counted using the increment operator declared in the for loop, and the results are printed accordingly.
Output
In the below output, the sum of the numbers is printed from 0 to 25 as “325”.
This brings an end to the discussion on the difference between prefix and postfix increments in Java.
Conclusion
The difference between the prefix and postfix increment in Java is that the prefix increment returns the value of the variable incremented by 1 and the postfix increment returns the original value and then increments it by 1. This article effectively discusses the difference between pre-increment(++i) and post-increment(i++) in Java.