Unitary operators in java are symbols that applies to a set of variables. The operator returns a result. A simple example is the one we know from mathematics. In a + b, the operator is the + sign.
Java has unary operators. These operators requires exactly one operand or variable. Examples of such operators are the following:
- ! : These inverts a boolean’s logical value.
- + : Indicates that a number is positive number.
- – : Indicates that a number is a negative number.
- ++: Increments a value by 1.
- — : Decrements a value by 1.
Logical Complement Operator
The operator ! flips the value of a boolean expression. If the boolean is true, the ! will convert it to false and vice versa. Let’s have some examples:
boolean isCar = true; System.out.println(isCar); // will print true isCar = !isCar; System.out.println(isCar); // will print false
The logical complement operator will not work with numeric expressions. For Example the following is not valid in java:
boolean blue = !5; // THIS DOES NOT COMPILE
The negation operator (-) reverses the sign of a numeric expression. This means a negative number becomes positive and vice versa. Here are some examples:
double temperature = 21.2; System.out.println(temperature); // WILL PRINT 21.2 temperature = -temperature; System.out.println(temperature); // WILL PRINT -21.2 int altitude = 100; System.out.println(altitude); // WILL PRINT 100 altitude = -(-altitude); System.out.println(altitude); // WILL PRINT 100
In the last example I used parentheses to apply the negation twice. — is not used here. That is the decrement operator and will decrement the value by 1.
This operator will not work with boolean expressions. The following example is not valid java code:
boolean isValid = !1; // This will not compile
Increment and Decrement Operators
The increment and decrement operators applies to a numeric variable. You place them before or after a variable. Depending on the place, the expression is different. For example:
int number = 0; System.out.println(number); // prints 0 System.out.println(++number); // prints 1 System.out.println(number); // prints 1 System.out.println(number--); // prints 1 System.out.println(number); // prints 0
The first pre-increment operator updates the value first. After that, it prints the value. The second post-decrement operator prints first the value. Afterwards it decrements the value by 1.
Some tricky things with these operators is the following:
int number = 3; int value = ++number * 5 / number--; System.out.println(number); // prints 3 System.out.println(value); // prints 5
This example will change the value of number two times on the same line. Number increments first. Afterwards java multiplies number by 5. It becomes:
int value = 4 * 5 / number --; // number is 4
Then number decrements by 1. In the expression 4 is still used. So value will be :
int value = 4 * 5 / 4; // Value will be 5
This means that number is 3 in the end and value is 5;
Unitary Operators in java are not that difficult. Keep in mind that you use the correct type of the variables. The order does matter for the increment and decrement operators. Be aware of that.