What is ++ in Java

Every programming language offers some built-in operators that can be utilized for performing several operations. In the Java language, these operators are divided into groups like Arithmetic operators such as addition “+”, multiplication “*”, increment “++”, decrement “” and Logical operators: “&&”, Assignment operators: “=” and Comparison operators: “==”, “<”, “>”.

This post will guide you about the “++” increment operator and its uses in Java.

What is ++ in Java?

The “++” operator in Java is an “Arithmetic Increment” operator. This increment operator increases the specified variable’s value by one. It has two types:

    • Pre-increment
    • Post-increment

Let’s use the increment operator to understand the difference between these two types.

How to Use ++ as a Pre-increment Operator in Java?

In Pre-increment, the “++” operator is used before the operand, and its value is incremented before performing the specified task:

++x

 
Now, we will examine the functionality of the Pre-increment operator with the help of an example.

Example

In this example, first, we will create an integer type variable name “value1” and assign it a value that is “18”:

int value1=18;

 
After that, we will use “++” as a Pre-increment operator to print the incremented value:

System.out.println("Pre-Incremented Value:" + ++value1);

 

The output shows the original value as “18” and incremented value as “19” because the Pre-increment operator first increments the value and then prints it:


Let’s utilize “++” as a Post-increment operator in Java.

How to Use ++ as a Post-increment Operator in Java?

In Post-increment, the “++” operator is added next to the operand, and its value is incremented after performing the specified task:

x++

 
Now, check the functionality of the Post-increment operator with the help of an example.

Example

We will utilize the same “value1” variable and print out its original value on terminal:

System.out.println("Original Value:" + value1);

 
Then, use the Post-increment operator “++” in “System.out.println()” method to print the incremented value:

System.out.println("Post-Incremented Value:" + value1++);

 
Lastly, print the updated value of the “value1” variable:

System.out.println("Updated Value:" + value1);

 

The output of the above-given program will print three lines: the first line containing the original value and the third line comprising the updated value after the increment.

Do not let the second line confuse you. As mentioned earlier, the Post-increment operator increments the value after performing the specified task. Therefore, the “System.out.println()” method displayed the original value first, then the “++” increment operator added one to it, which can be seen in the third line:


We have provided all the essential information about the “++” operator and its usage in Java.

Conclusion

In Java, “++” is an Arithmetic Increment operator utilized for incrementing the value of a variable by one. This operator has two types: Pre-increment and Post-increment. The Pre-increment operator increments the value before performing the specified task, whereas the Post-increment performs the task first and updates the value later. This post explained what “++” is and how to use it in Java as a Pre-increment and Post-increment operator.



from https://ift.tt/dN3PnFv

Post a Comment

0 Comments