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 |
- |