This write-up will present a thorough understanding of the below-listed concepts:
- What does ++ Mean in Java
- ++ Operator as Pre-increment and Post-increment
- How Pre-increment and Post-increment Operators Work in Java
- What does — Mean in Java
- — Operator as Prefix and Postfix
- How Pre-decrement and Post-decrement Operators Work in Java
So let’s start!
What does ++ Mean in Java
The ++ sign is a unary operator known as an increment operator. It is used to increment/increase the value of a variable, and It can be used as either Prefix or Postfix.
++ Operator as Pre-increment and Post-increment
Pre-increment means: ++ operator will be used before the variable, i.e., “++ variable”. So, in the case of prefix increment, first, the variable’s value will be incremented/increased by one, then it will return that incremented value.
On the other hand, if we talk about Postfix increments, the ++ operator will come after the variable, i.e., “variable ++”. So, in this case, first, the variable’s original value will be returned, and then its value will be incremented by 1.
How Pre-Increment and Post-Increment work in Java
Consider the below code snippet, for a profound understanding of how prefix and postfix increment work in java:
In both cases (i.e. Prefix, Postfix) the value of variables should be incremented by 1:
From the above snippet, we observed that the prefix showed the incremented value, but the postfix showed the original value(non-incremented). Why is it so?
How to verify whether the value of the postfix operator is incremented or not?
In the previous section, we learned that the postfix operator first returned the variable’s original value and then incremented the variable’s value by 1. So, considering this condition, if we print the “num2” again, then the result must be the incremented value, so let’s try:
Below snippet will show the resultant output:
The output verified that when we fetched the value of num2, it showed the incremented value, which proved the working of the post-increment operator.
What does — Mean in Java
It is a unary operator referred to as a decrement operator in all programming languages, and it decrements/decreases the value of a variable by 1. The — operator can be used as either Prefix or Postfix.
— Operator as Prefix and Postfix
Pre-decrement means: — operator will be used before the variable, i.e., “– variable”. So, in the case of prefix decrement, first, the variable’s value will be decremented/decreased by one, then it will return that decremented value.
On the other hand, if we talk about Postfix decrement, the –operator will come after the variable, i.e., “variable –”. So, in this case, first, the variable’s original value will be returned, and then its value will be decremented by 1.
How Pre-decrement and Post-decrement work in Java
Let’s consider the below example to understand the working of prefix and postfix decrement operators in java:
The above-given code snippet will produce the following output:
Output verified the working of pre-decrement and post-decrement operators.
Conclusion
In java, ++ and — signs represent increment and decrement operators, respectively. The ++ and — operators respectively increase and decrease the variable’s value by 1. Both these operators can be used as either prefix or postfix. In prefix, the increment/decrement operator comes before the variable, while in the case of postfix, the increment/decrement operator comes after the variable. The prefix operators first increment/decrement the variable’s value by one, then return that incremented value. While the postfix operators first return the variable’s original value and then increment/decrement the variable’s value by 1.