The word Unary means an operation that involves a single element. As the name suggests, The Unary operators in Java involve single operand. Java supports following unary operators:
- Unary minus(-)
- Increment(++)
- Decrement(- -)
- NOT(!)
- Bitwise Complement(~)
1. Unary minus(-) Operator Example
The unary minus operator changes the sign of the operand. This operator is used on numbers, it changes the positive number to negative number and vice-versa.
int num = 100; int num2 = -num; //value of num2 is -100
Difference between – arithmetic operator and – unary operator:
The – arithmetic operator involves two operands while the – unary operator involves a single operand.
int x = 10, y = 5, z = 100, sub; //this is minus(-) arithmetic operation sub = x - y; //this is minus (-) unary operation int num = -z; //value of num is -100
Example of unary minus:
class JavaExample { public static void main(String[] args) { int number = 101; //opposite of number. The number with opposite sign is also called //additive inverse. The sum of number and it's additive inverse is zero int numberInverse = -number; System.out.println("The value of number is: "+number); System.out.println("Opposite of number is: "+numberInverse); } }
Output:
2. Increment(++) Operator Example
The increment(++) operator is used to increase the value of a variable. It can be used in following two ways:
- Pre-increment (++i)
- Post-increment (i++)
class JavaExample { public static void main(String[] args) { int number = 5; //increment unary operator number++; System.out.println(number); } }
Output:
6
Difference between pre increment and post increment in java:
In pre-increment the value is incremented instantly, however in post-increment the value is incremented at the end of this statement and before executing next statement. This is shown in the following example:
Here the first if statement didn’t execute as the value of num1 is still 5 during comparison and only got increment after the condition is already evaluated. The second if statement executed because pre-increment increased the value instantly before reading the remaining statement, this made the value of num2 to 6 before comparison.
class JavaExample { public static void main(String[] args) { int num1 = 5, num2 = 5; //post-increment if(num1++ == 6){ System.out.println("First if block"); System.out.println("Value of num1: "+num1); } //pre-increment if(++num2 == 6){ System.out.println("Second if block"); System.out.println("Value of num2: "+num2); } } }
Output:
3. Decrement(- -) Operator Example
The decrement(- -) operator is used to decrease the value of an operand. Similar to ++ operator, it can also be used in following two ways:
- Pre-decrement (- -i)
- Post-decrement (i- -)
class JavaExample { public static void main(String[] args) { int number = 5; //decrement unary operator number--; System.out.println(number); } }
Output:
4
Difference between pre decrement and post decrement in java:
In pre-decrement the value is decremented instantly, however in post-decrement the value is decremented at the end of this statement and before executing next statement. For example:
class JavaExample { public static void main(String[] args) { int num1 = 5, num2 = 5; //post-decrement if(num1-- == 4){ System.out.println("Post-decrement Operator"); System.out.println("Value of num1: "+num1); } //pre-decrement if(--num2 == 4){ System.out.println("Pre-decrement Operator"); System.out.println("Value of num2: "+num2); } } }
Output:
4. NOT(!) Operator Example
The NOT(!) Operator reverses the logical state (true or false) of an operand. If an operand or condition is true, then the Logical NOT operator will make it false and vice-versa.
If value of a boolean variable 'b' is true, then !b is false If the value of 'b' is false, then !b is true
Let’s take an example to understand this concept:
class JavaExample { public static void main(String[] args) { int totalMarks = 32; //if total marks are not greater than 33 then //the student is fail else pass if(!(totalMarks > 33)){ System.out.println("Fail"); } else{ System.out.println("Pass"); } } }
Output:
Fail
5. Bitwise Complement(~) Operator Example
Bitwise complement is represented by ~ (Tilde) symbol. The bitwise complement is a 1’s complement representation of number. 1’s complement changes the 1’s to 0’s and 0’s to 1’s.
For example: In the following java program, we got -8 as a bitwise complement of 7, let’s see how we got this value.
Binary representation of number 7 = 111
1’s complement of this: 000 (reversed 1’s to 0’s and 0’s to 1’s)
decimal equivalent of this: 0
The compiler returns a 2’s complement of an input number, this means that compiler will print the 2’s complement of 0.
2’s complement= 1’s complement + add 1 to Least Significant Bit (LSB).
= 111 + 1
= 1000
Decimal equivalent of this 2’s complement is -8.
class JavaExample { public static void main(String[] args) { int num = 7; //print bitwise complement of 7 System.out.println(~num); } }
Output:
-8