What are Unary Operators in Java?
Operators in Java are an essential part of any programming language. They allow developers to perform various calculations and operations, such as arithmetic, logical, and relational. Java, like many other programming languages, offers a wide range of operators that can be used to execute various tasks and make programming more efficient.
Unary operators only require one operand to operate. These operations can include incrementing or decrementing a value, reversing a boolean value, or other types of calculations. Examples of unary operators include the increment operator (++), decrement operator (--), and negation operator (!). It is important to note that Unary operators can be applied to different types like arithmetic, logic, and others.
Types of Unary operators are:
Types of Unary Operators in Java
Let's see different types of unary operators in Java :
Symbol | Operator Name | Description |
---|---|---|
- | Unary Minus | It is used to denote a negative value. |
+ | Unary Plus | It is used to denote a positive value. |
- - | Decrement Operator | Decrements value by 1 |
++ | Increment Operator | Increments value by 1 |
! | Logical Complement Operator | Reverse the value of boolean variable |
Unary Plus
It is used to represent a positive operand. In this, the operator is optional; that is, we do not have to write the operator before the operand.
Syntax:
Example:
Unary Minus
It converts a positive value into a negative value and vice versa.
Syntax:
Example:
Explanation:
- In the code above, the value of x1 was initially 12 before the unary minus(-) operator was applied to it. As a result, it changed to -12 since the unary operator negated its value.
Output:
Increment Operator
The increment operator in Java increases the value of the variable(operand) by one(1). The symbol for the operator is two plus signs (++). The operator can be applied before or after the operand.
In java, the increment operator is used in two forms:
- Pre-increment operator (Prefix)
- Post-increment operator (Postfix)
Pre-increment Operator:
The pre-increment operator is defined as when the increment operator(++) is written before a variable, then it is known as the Pre-increment operator or Prefix. The Pre-increment operator works by incrementing the value by one(1) before returning the new incremented value.
Syntax:
Example:
Explanation:
- In the very first statement, a value 12 has been assigned to a.
- Then, in the next statement, it performs two actions.
- It increments the value of a to 13.
- Then assign the new incremented value to b.
Output:
Post-increment Operator:
The post-increment operator is defined as when the increment operator(++) is written after a variable. It is also known as the Post-increment operator or Postfix. The Post-increment operator works by initially returning the value of the operand and finally incrementing it by one(1).
Syntax:
Example:
Explanation:
- In the above code, we can see that, first, the post-increment operator assigns the value in the left-hand side variable b.
- Then the operator increments the value of a by 1.
Output:
Note: Both i += 1 and i++ evaluate the same result but the only difference between them is that i++ uses the increment operator(unary operator) while i+=1 uses the assignment operator.
Decrement Operator
Decrement operator in java is used to decrement the value of operand by one(1). The symbol for the operator is two minus signs (--). Same as of increment operator it can also be applied before and after an operand.
In java, the decrement operator is used in two forms:
- Pre-decrement operator (Prefix)
- Post-decrement operator (Postfix)
Pre-decrement Operator:
The pre-decrement operator is defined as when the decrement operator(--) is written before a variable; then, it is known as the Pre-decrement operator or Prefix. The Pre-decrement operator works so that, first the value is decremented by one(1), then returns the new decremented value.
Syntax:
Example:
Explanation:
- In the very first statement, a value 12 has been assigned to a.
- Then in the next statement, it performs two actions.
- It decrements the value of a to 11.
- Then assign the new decremented value to b.
Output:
Post-decrement Operator:
Post-decrement operators, also known as Postfixes, are created when the decrement operator (--) is written after a variable. The Post-decrement operator works in such a way that, the operator first returns the value of the operand and then at the end, the value is decremented by one(1).
Syntax:
Example:
Explanation:
- In the above code we can see that, first the post-decrement operator assign the value in the left hand side variable b.
- Then the operator decrements the value of a by 1.
Output:
Logical Complement Operator
A logical Complement Operator is used to reverse the value of a Boolean value. It means that if the value of an operand is true, then the complement of the operator will be false and vice-versa. It is represented by an exclamatory symbol (!).
Syntax:
Example:
Explanation:
- In the code above, first, we declare two Boolean variables that are Var1 and Var2.
- Then Logical Complement Operator (!) applies on Var1 and updated the value of Var1.
- Then Logical Complement Operator (!) applies on the Var2 and updated the value of Var2.
- The Boolean values of the variables are now reversed.
Output:
Conclusion
- You can modify the sign, conduct an increment or decrement, or change the Boolean value (true/false) using the unary operators.
- Unary Plus is used to represent positive values.
- Unary Minus is used to convert positive value to negative value.
- Increment operator is used to increment the value of a variable by one(1).
- Decrement operator is used to decrement the value of a variable by one(1).
- Logical Complement Operator is used to reverse the boolean value from true to false and vice-versa.