Introduction to Unary Operators in Java
For any programming language, a wide range of operators, methods, and functions are available to be used according to the need. Class-based, Object-Oriented Programming Language, Java, provides a wide range of Operators, and one such type of operators in Java are “Unary Operators.”
Unary Operators can be simply defined as an operator that takes only one operand and does a plain simple job of either incrementing or decrementing the value by one. Added, 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 abilities 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 those of binary operators, which accepts two operands. These operators are like special symbols that are used to execute certain operations on 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’ve assigned the num to the Unary Plus operator. And 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 then incremented. In post-increment, whatever the value is, it is first used for computing purpose, 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: While using the decrement operator in post form, the value is first used 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. We’ve firstly passed the decrement operator to the variable, as the num—and the value will be printed. Later we’ve passed 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”. Meaning it will first be decremented to 5 and then incremented by 1, making it 6 again.
Output:
5. Logical Complement Operator
This operator is simply 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 have demonstrated a Logical Complement operator here, 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 have printed the original value, and later we have passed the logical complement operator; as you can see, we’ve used the “!” symbol. And with this implementation, the value of the boolean variable will be inverted, resulting in the changed output, which will be true.
Output:
Below are the cases, which if executed, will result in errors:
- It is important to understand the fact that these increment and decrement operators can only be used with variable and not with constant values. Implementing these operators with contact values will result in an error. Another interesting point is how an unexpected error will occur in case 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. Now 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: These operators can be implemented on all types of primitive data types, excluding Boolean values.
- In case of any of the above-mentioned scenarios, 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 explanation, we have programs for respective operators and screenshot along with code interpretation. And some important 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 –