Unary Operators in Java
In Java, the unary operator is an operator that can be used only with an operand. It is used to represent the positive or negative value, increment/decrement the value by 1, and complement a Boolean value. In this section, we will discuss the unary operator in Java with examples and also understand the differences between i++ and i+=1.
There are five unary operators in Java:
- Unary Plus
- Unary Minus
- Increment Operator
- Decrement Operator
- Logical Complement Operator
The following table describes the short description of the unary operators.
Operator Name | Symbol | Description | Example | Equivalent Expression |
---|---|---|---|---|
Unary Plus | + | It is used to represent the positive value. | +a | a |
Unary Minus | – | It is used to represent the negative value. | -a | – |
Increment Operator | ++ | It increments the value of a variable by 1. | ++a or a++ | a=a+1 |
Decrement Operator | — | It decrements the value of a variable by 1. | –a or a– | a=a-1 |
Logical Complement Operator | ! | It inverts the value of a boolean variable. | !true | – |
Unary Plus
It is used to represent positive values. Usually, we do not write the operator before the operand. Hence, it is optional.
Syntax:
Example: x=+99 or x=99;
Unary Minus
It is used to convert a positive value into a negative one.
Syntax:
Increment Operator
It is used to increment the value of an operand by one. The operator is represented by a pair of plus operators (++). The operator can be applied before or after an operand.
Pre-increment Operator: If an increment operator is written before (prefix) the operand is known as pre-increment. In such a case, the value is first incremented by 1 and then taken for the computing purpose.
Syntax:
Example: Suppose x=9, then the value of ++x will be 10.
Post-increment Operator: If an increment operator is written after (postfix) the operand is known as post-increment. In such a case, the value is first processed and then incremented. It means the value that the variable holds first used for the computing purpose and then incremented by 1.
Syntax:
Example: Suppose x=11, then the value of x++ will be 12.
Let’s use the pre-increment and post-increment operators in a Java program.
PrePostIncrementExample.java
Output:
11 12 13 13 14
What is the difference between i++ and i+=1?
Both the expressions i++ and i+=1 evaluated the same result. But the only difference is that i++ uses the increment operator (unary operator) and i+=1 uses the assignment operator.
Another difference is that the expression i++ directly increments the value of i but the expression i+=1 first converts into i=i+1 by the JVM and then executed.
Decrement Operator
It is used to decrement the value of an operand by 1. The operator is represented by a pair of minus operators (–). The operator can be applied before or after an operand.
Pre-decrement Operator: If a decrement operator is written before (prefix) the operand is known as pre-decrement. In such a case, the value is first decremented by 1 and then used for the computing purpose.
Syntax:
Example: Suppose x=9, then the value of –x will be 8.
Post-decrement Operator: If a decrement operator is written after (postfix) the operand is known as post-decrement. In such a case, the value is first used and then decrements by 1.
Syntax:
Example: Suppose x=11, then the value of x– will be 10.
Let’s use the pre-decrement and post-decrement operators in a Java program.
PrePostDecrementExample.java
Output:
19 18 17 17 16
Remember
- Nesting of increment or decrement operator is not allowed. For example:
- Never use unary operators with the final
- The expressions ++x and x++ is the same as x=x+1 and the expressions – -x and x- – is the same as x=x-1.
Logical Complement Operator
It is used to reverse the value of an operand. It means if an operand has value true, the complement if the operator will be false and vice-versa. It is represented by the exclamatory symbol (!).
Syntax:
Example: flag=!true;
Example of Unary Operator
In the following example, we have used all the unary operators that we have discussed above.
UnaryOperatorExample.java
Output:
76 -76 21 20 false true