Java unary operators are the types that need only one operand to perform any operation like increment, decrement, negation, etc. It consists of various arithmetic, logical and other operators that operate on a single operand.
Operator 1: Unary minus(-)
This operator can be used to convert a positive value to a negative one.
Syntax:
~(operand)
Program
Operator 2: ‘NOT’ Operator(!)
This is used to convert true to false or vice versa. Basically, it reverses the logical state of an operand.
Syntax:
!(operand)
Operator 3: Increment(++)
It is used to increment the value of an integer. It can be used in two separate ways:
1: Post-increment operator
When placed after the variable name, the value of the operand is incremented but the previous value is retained temporarily until the execution of this statement and it gets updated before the execution of the next statement.
Syntax:
num ++
Example
num = 5;
num ++ = 6
2: Pre-increment operator
When placed before the variable name, the operand’s value is incremented instantly.
Syntax:
++ num
Example
num = 5;
++ num = 6
Operator 4: Decrement(–)
It is used to decrement the value of an integer. It can be used in two separate ways:
1: Post-decrement operator
When placed after the variable name, the value of the operand is decremented but the previous values is retained temporarily until the execution of this statement and it gets updated before the execution of the next statement.
Syntax:
num --
Example
num = 5;
num -- = 4
2: Pre-decrement operator
When placed before the variable name, the operand’s value is decremented instantly.
Syntax:
-- num
Example
num = 5;
-- num = 4
Operator 5: Bitwise Complement(~)
This unary operator returns the one’s complement representation of the input value or operand, i.e, with all bits inverted, which means it makes every 0 to 1, and every 1 to 0.
Syntax:
~ (Operand)
Example
a = 5 [0101 in Binary]
result = ~5
This performs a bitwise complement of 5
~0101 = 1010 = 10 (in decimal)
Then the compiler will give 2’s complement of that number.
2’s complement of 10 will be -6.
result = -6
No comments:
Post a Comment