Updated June 13, 2023
Introduction to Unary Operators in Java
For any programming language, a wide range of operators, methods, and functions are available for use according to the need. Class-based, Object-Oriented Programming Language, Java, provides a wide range of Operators, and one such type of operator in Java is “Unary Operators.”
Unary Operators can be an operator that takes only one operand and does a simple job of either incrementing or decrementing the value by one. Additionally, Unary operators also perform Negating operations for expression, and the value of the boolean can be inverted.
Types of Unary Operators
There are five unary operators with the ability to perform various operations. Given below is the list of five Unary Operators:
- Unary Plus, denoted by “+.”
- Unary minus, denoted by “-“
- Unary Increment Operator, denoted by “++.”
- Unary Decrement Operator, denoted by “–“
- Logical Complement Operator, denoted by “!”
Unary Operators are quite different from binary operators, which accept two operands. These operators are like special symbols used to execute certain operations on an operand; operand here are variables and values.
1. Unary Plus
Simply returns the value as positive. Whatever the value is, unary plus will not return a negative form.
2. Unary Minus
Like Plus operators return the positive value, Unary Minus returns the same value’s negative form. For the above-explained unary operators, we will demonstrate an example where we will implement the unary plus and minus operators.
Code:
class unary_ops {
public static void main(String[] args) {
int num = 6;
num = +num;
System.out.println(num);
num = -num;
System.out.println(num);
}
}
Code Interpretations: We have demonstrated plus and minus unary operators in the above example. We have our class, then the main class within, and we have declared a simple integer with a value of 6. Then we assigned the num to the Unary Plus operator. And we later printed the result, which will be simple plain 6. Then we’ve passed the same variable to the Unary Minus operator, and the value changes here. We’ve printed the output with the print statement, which is expected to be -6, meaning negative 6. Upon executing the above code, 6 and -6 are the expected output.
Output:
3. Unary Increment Operator
Just as the name suggests, this unary operator does the operation of incrementing the value by 1. Whatever the value of a variable, after it is passed with the increment operator, the value is incremented by 1. Unary Increment operator can be later categorized into two types, based on when the operation of increment happens:
- Post-Increment: Value is first processed and then incremented. In post-increment, whatever the value is, it is first used for computing purposes, and after that, the value is incremented by one.
- Pre-Increment: On the contrary, Pre-increment does the increment first, then the computing operations are executed on the incremented value.
4. Unary Decrement Operator
Like the Increment operator increases the value by one, the Unary Decrement Operator decreases the variable value by 1.
Similar to the increment operator, the Decrement operator has two varieties:
- Post-Decrement: Using the decrement operator in post form, the value is first used and then updated.
- Pre-Decrement: With prefix form, the value is first decremented and then used for any computing operations.
Demonstrate the use of the above-mentioned increment and decrement operators.
Code:
class unary_ops {
public static void main(String[] args) {
int num = 6;
num--;
System.out.println(num);
num++;
System.out.println(num);
}
}
Code Interpretation: Same class with the main class within, integer num with the value of 5. First, We passed the decrement operator to the variable, as the num—and the value will be printed. Later we pass the same calculated value to the increment operator, and the result will be printed. Our original value is 6, and upon execution, the output will be “5 6”. It will decrement to 5 first and then increment by 1, bringing it back to 6 again.
Output:
5. Logical Complement Operator
This operator is used to invert the boolean value of any variable. Ex. If a variable’s boolean value is true, it’ll be inverted to false after it’s passed with a logical operator.
Code:
class unary_ops {
public static void main(String[] args) {
boolean bvalue = false;
System.out.println(bvalue);
System.out.println(!bvalue);
}
}
Code Interpretation: We demonstrated a Logical Complement operator using the Boolean data type. In our class, we have the main class within and our simple boolean variable, which holds the value of false. In our first print statement, we printed the original value and later passed the logical complement operator; as you can see, we’ve used the “!” symbol. This implementation will invert the value of the boolean variable, resulting in a changed output of true.
Output:
Below are the cases, which if executed, will result in errors:
- It is important to understand that these increment and decrement operators can only be used with variables, not constant values. Implementing these operators with contact values will result in an error. Another interesting point is how an unexpected error will occur if you use these operators in nested form. Not acceptable nested form is: int x = ++(++y);
- Operations on Final Variables: Declaring a final variable is as easy as adding a final keyword before the data type, which results in a type variable whose value cannot be changed. Keeping this in mind, we cannot implement these operators on the final variable, as these operations result in a change in value.
- For Boolean values: We can implement these operators on all types of primitive data types except Boolean values.
- In case of any of the scenarios mentioned above, it will result in errors.
Conclusion
There are 5 unary operators and with pre and post as two varieties. We understood each operator with a specific definition and usage. Along with an explanation, we have programs for respective operators, screenshots, and code interpretation. And some essential tips to wisely implement these operators.
Recommended Articles
This has been a guide to Unary Operators in Java. Here we discuss the introduction and different types of unary operators in Java. You may also have a look at the following articles to learn more –