Home >Java >javaTutorial >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.”
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
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.
There are five unary operators with the ability to perform various operations. Given below is the list of five Unary Operators:
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.
Simply returns the value as positive. Whatever the value is, unary plus will not return a negative form.
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:
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:
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:
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:
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:
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.
The above is the detailed content of Unary Operators in Java. For more information, please follow other related articles on the PHP Chinese website!