Unary Operators in Java
-
Last Updated: September 13, 2023
-
By: javahandson
-
Series
In this article, we will learn what is an unary operator and what are different unary operators in Java with proper examples. If you want to learn about other Java topics you can click the above Java menu or you can click on this link Java topics.
A unary operator is an operator that operates on a single operand. Unary operators mostly operate on numbers. The unary operator uses only a single variable or a literal.
Operator | Operator Name | Purpose |
+ | Unary plus | It does not have any impact on the value. It is like multiplying the number with +1 |
– | Unary minus | It negates the value which means converting the positive value to the negative one. It is like multiplying the number with -1 |
++ | Incremental | Increases the value by 1 |
— | Decremental | Decreases the value by 1 |
! | Negate | It inverts the boolean value. If the value of a boolean variable is true then negate operator converts its value to false and vice versa. |
Here we will take one positive operand and one negative operand and check how Unary plus and Unary minus operate on them.
package com.java.handson; public class UnaryOperators { public static void main(String[] args) { int result = 10; // positive operand int unaryPlusResult = +result; System.out.println("Value of unaryPlusResult is : "+ unaryPlusResult); int unaryMinusResult = -result; System.out.println("Value of unaryMinusResult is : "+ unaryMinusResult); int output = -20; // negative operand int unaryPlusOutput = +output; System.out.println("Value of unaryPlusOutput is : "+ unaryPlusOutput); int unaryMinusOutput = -output; System.out.println("Value of unaryMinusOutput is : "+ unaryMinusOutput); } } Output: Value of unaryPlusResult is : 10 Value of unaryMinusResult is : -10 Value of unaryPlusOutput is : -20 Value of unaryMinusOutput is : 20
From the above example, it is clear that the Unary plus operator doesn’t affect the initial value of the operand because the value of the result and output variables are unchanged. But the Unary minus affects the initial value of the operands as we can see 10 becomes -10 and -20 becomes 20.
Incremental operators will be of 2 types i.e. Post-Increment and Pre-Increment
a++ = a + 1
The post-Increment operator will first perform the task and then evaluate the value i.e. increment the value by 1.
int result = 10; System.out.println("value of result is : "+ result++); System.out.println("value of result is : "+ result); Output: value of result is : 10 value of result is : 11
In Line 3 print operation is executed first then the value of the result is incremented by 1. This is the reason the value of the result is printed as 10 in the first print statement whereas in the second print statement, the incremented value of the result is printed i.e. 11.
++a = a + 1
The pre-increment operator will first evaluate the value i.e. increment the value by 1 and then perform the task.
int output = 20; System.out.println("value of output is : "+ ++output); System.out.println("value of output is : "+ output); Output: value of output is : 21 value of output is : 21
In Line 3 value of the output variable is evaluated first i.e. incremented by 1 then the print operation is executed. This is the reason the value of output is printed as 21 in the first print statement as well as the second print statement.
package com.java.handson; public class UnaryOperators { public static void main(String[] args) { int result = 10; System.out.println("value of result is : "+ result++); System.out.println("value of result is : "+ result); int output = 20; System.out.println("value of output is : "+ ++output); System.out.println("value of output is : "+ output); } } Output : value of result is : 10 value of result is : 11 value of output is : 21 value of output is : 21
Decremental operators will be of 2 types i.e. Post-Decrement and Pre-Decrement.
a-- = a - 1
The post-decrement operator will first perform the task and then evaluate the value i.e. decrement the value by 1.
int result = 10; System.out.println("value of result is : "+ result--); System.out.println("value of result is : "+ result); Output: value of result is : 10 value of result is : 9
In Line 3 print operation is executed first then the value of the result is decremented by 1. This is the reason the value of the result is printed as 10 in the first print statement whereas, in the second print statement, the decremented value of the result is printed as 9.
--a = a - 1
The pre-decrement operator will first evaluate the value i.e. decrement the value by 1 and then perform the task.
int output = 20; System.out.println("value of output is : "+ --output); System.out.println("value of output is : "+ output); Output: value of output is : 19 value of output is : 19
In Line 3 value of the output variable is evaluated first i.e. decremented by 1 then the print operation is executed. This is the reason the value of output is printed as 19 in the first print statement as well as the second print statement.
package com.java.handson; public class UnaryOperators { public static void main(String[] args) { int result = 10; System.out.println("value of result is : "+ result--); System.out.println("value of result is : "+ result); int output = 20; System.out.println("value of output is : "+ --output); System.out.println("value of output is : "+ output); } } Output : value of result is : 10 value of result is : 9 value of output is : 19 value of output is : 19
package com.java.handson; public class UnaryOperators { public static void main(String[] args) { boolean isValid = false; if (!isValid) { System.out.println("Inverted isValid value to true so as to execute this step"); } } } Output : Inverted isValid value to true so as to execute this step
In the above example isValid boolean was false but when we apply the negate operator the value of isValid is converted to true and hence the if condition gets executed.
So this is all about Unary operators in Java. I hope you like the article. If you have any questions on this topic please raise them in the comments section.