Increment ++ and decrement are unary operators in Java that increase/increment and decrease/decrement the variable’s value by 1, respectively. As the increment is a unary operator, it works/operates on a single operand. By default it increments the variable’s value by 1, however, you can customize the increment value as per your needs. In this Java guide, we will walk you through different use cases of the increment operator via practical examples.
Quick Outline
- How to Use Increment ++ Operator in Java
- Prefix Increment vs. Postfix Increment in Java: What’s the Difference
- When to Use Prefix and When to Use Postfix Increment in Java
- How to Use Decrement Operator in Java
- Bottom Line
How to Use Increment ++ Operator in Java
The increment operator is used either before or after the variable name like this:
++varName; //pre-increment
varName++; //post-increment
The one that is used before the variable name is referred to as the “pre-increment” while the other one is called a “post-increment” operator. Both operators increment the variable’s value by 1, however, both of them differ in terms of working/usage.
Important: The increment operator (++varName or varName++) and “varName + 1” are equivalent.
Example 1: Understanding the Working of Increment Operator in Java
In the below code, we create a class “IncrementJava” to show you the basic usage of the increment operator (i.e., the pre-increment and post-increment):
public class IncrementJava {
public static void main(String args[]) {
int inputNum = 72;
System.out.println("The Original Value: " + inputNum);
int preIncrementNum = ++inputNum; System.out.println("The Pre Increment Value: " + preIncrementNum);
int postIncrementNum = inputNum++;
System.out.println("The Post Increment Value: " + postIncrementNum);
System.out.println("The Modified inputNum Value: " + inputNum);
}
}
Let’s understand how the above code works in Java:
- First, we declare a variable “inputNum”, initialize it with an integer, and print it on the console.
- After this, we apply the pre-increment operator on the given “inputNum” and store its result in a variable “preIncrementNum”. Also, we print the incremented value on the console.
- Up next, we apply the post-increment operator on the original “inputNum” variable and store the incremented value in the “postIncrementNum” variable.
- Finally, we print the post-increment value and the modified inputNum value on the console.
The output shows that both pre and post-increment operators increment the value of inputNum by 1:
Example 2: How to Increment a Variable by 2 in Java
From the above example, we have seen that the increment operator increments the variable’s value by 1 (by default). But what if you have to increment the value by 2 or some other number? Well! You can do this by using the “addition assignment operator (+=)”:
public class IncrementJava { public static void main(String args[]) {
int inputNum = 72;
System.out.println("The Original Value: " + inputNum);
System.out.println("Increment Variable’s Value by 2: " + (inputNum+=2)); //output ==> 72+2 =74
System.out.println("Increment Variable’s Value by 10: " + (inputNum+=10)); //output ==> 74+10 =84
}
}
In this code,
- We declare a variable “inputNum” and initialize it with an integer “72”.
- Next, we employ the addition assignment operator “+=” to increment the “inputNum” value by 2.
- After incrementing the variable’s value by 2, the current value of the “inputNum” variable will become “74”. This means the next increment operation will be performed on this updated/incremented value.
- Finally, we use the addition assignment operator one more time to increment the variable’s value by “10”. Here’s the resultant outcome:
Example 3: How to Use Increment Operator in Loops
The most common use case of increment and decrement operators is in loops. Programmers use loops to iterate a specific data structure, collection, etc. For instance, in “Arrays”, data is stored in contiguous indexes, so it is traversed index by index. In such cases, we need to increment the value of the variable/counter to iterate all the array indexes. Here is a practical demonstration of the stated concept:
public class IncrementJava {
public static void main(String args[]) {
String inputArray[] = { "Welcome", "to", "JavaBeat", ".net" };
for (int i = 0; i< inputArray.length; i++) {
System.out.println("Array Value at index " + i + " is ==> " + inputArray[i]);
}
}
}
In this example,
- Initially, we have a string-type array “inputArray” containing different strings.
- We use the for loop to iterate each index of the given array. Note that, in the loop, we use the increment operator to traverse the next array index.
- Within the loop, we use the println() method to print each array element on the console.
Example 4: Can We Invoke the Increment Operator on a Final or Constant Value
No! If you do so, you will encounter a compile-time error stating “invalid argument to operation ++/–”:
Similarly, if you try to increment or decrement a final variable, you will face a compile-time error stating “final variable can’t be assigned”:
Prefix Increment vs. Postfix Increment in Java: What’s the Difference
The post-increment increments the variable’s value by 1 but retrieves the original value. While the pre-increment increments and retrieves the incremented value. The below example will assist you in understanding the difference between pre and post-increment:
public class IncrementJava {
public static void main(String args[]) {
// initializing a variable
int inputNum = 5;
//applying post-increment and printing the resultant value
System.out.println("Value of Post Increment: " + inputNum++);
// initializing another variable
int inputVal = 5;
//applying pre-increment and printing the resultant value
System.out.println("Value of Pre Increment: " + ++inputVal);
}
}
Executing the above code will produce the following results:
But how can we validate if the post-increment operator has actually incremented the variable value or not? Well! We can print the variable’s value one more time to ensure the working of the post-increment operator, as shown below:
public class IncrementJava {
public static void main(String args[]) {
// initializing a variable
int inputNum = 5;
//applying post-increment and printing the resultant value
System.out.println("Actual Value of Post Increment: " + inputNum++);
System.out.println("Incremented Value of Post Increment: " + inputNum);
}
}
The output authenticates that the post-increment has successfully incremented the variable’s value:
When to Use Prefix and When to Use Postfix Increment in Java
Use the post-increment in your code if you want to use the original value before incrementing it by 1. Conversely, you can use the pre-increment if you want to employ the incremented value in your code.
How to Use Decrement Operator in Java
Decrement “–” is also a unary operator that decrements the variable’s value by 1. Similar to the increment operator, the decrement operator also has two variants/types “pre-decrement” and “post-decrement”, as shown below:
--variableName; //pre-decrement
variableName--; //post-decrement
Let’s learn the workings of the decrement operator using the following code example:
public class IncrementJava {
public static void main(String args[]) {
int inputNum = 72;
System.out.println("The Original Value: " + inputNum);
int preDecrementNum = --inputNum;
System.out.println("The Pre Decrement Value: " + preDecrementNum);
int postDecrementNum = inputNum--;
System.out.println("The Post Decrement Value: " + postDecrementNum);
System.out.println("The Modified inputNum Value: " + inputNum);
}
}
In the above code,
- We declare an integer and apply pre as well as post-decrement operators on it.
- Also, we print the original, pre-decremented, post-decremented, and modified variable values on the console.
Following will be the resultant values of the decrement operator:
This is how the increment and decrement operators work in Java.
Bottom Line
Increment “++” is one of the unary operators in Java that increments the variable value by one. You can use the increment operator either as a pre-increment (++variableName) or as a post-increment (variableName++). The pre-increment increments/increases and retrieves the incremented value while the post-increment retrieves the variable’s current value before incrementing it. You can use any of the mentioned variants according to your program’s requirements.