Java

What does ++ and — mean in java

Java provides various types of operators to perform different operations on variables/values, for example, unary operators, logical operators, arithmetic operators, etc. The ++ and — are increment and decrement operators in java that belong to the category of unary operators and can be categorized as prefix and postfix. The prefix and postfix operators show the precedence for the variables/expressions.

This write-up will present a thorough understanding of the below-listed concepts:

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:

public class IncrementExample {

public static void main(String[] args) {

int num1 = 50, num2 = 50;

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

System.out.println("Post Increment: " + (num2++));

}

}

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:

public class IncrementExample {

public static void main(String[] args) {

int num1 = 50, num2 = 50;

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

System.out.println("Post Increment: " + (num2++));

System.out.println("Post Increment: " + (num2));

}

}

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:

public class IncrementExample {

public static void main(String[] args) {

int num1 = 50, num2 = 50;

System.out.println("Pre Decrement: " + (--num1));

System.out.println("Post Decrement: " + (num2--));

System.out.println("Post Decrement: " + (num2));

}

}

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.

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.