The unary operators requires only one operand to perform different kind of operations such as increasing/decreasing a value, negating an expression, or inverting a boolean value.
Unary Operators
The unary operators requires only one operand to perform different kind of operations such as increasing/decreasing a value, negating an expression, or inverting a boolean value. These operators can not be used with final variables. There are different types of unary operators mentioned in the table given below:
Symbol | Name of the Operator | Operation | Example |
+ | Unary plus operator | indicates positive value (however, numbers are positive without this) |
int number = +1; |
- | Unary minus operator | negates an expression | number = - number; |
++ | Increment operator | increments a value by 1 | number = ++ number; |
-- | Decrement operator | decrements a value by 1 | number = -- number; |
! | Logical compliment operator | inverts a boolean value |
I. Unary Plus (+) Operator
Unary plus operator (+) indicates positive value. This (+) operator is used to perform a type conversion operation on an operand. The type of the operand must be an arithmetic data type i.e. if a value of the integer operand is negative then that value can be produced as a positively applying unary plus (+) operator. For example, lets see the expressions shown as:
int x = 0; int y = (-25); x = (+y); |
In this expression, a negative value is assigned
to the variable "y". After applying unary plus
(+) operator on the operand "y", the value becomes 25 which
indicates it as a positive value.
However a number is positive without using unary plus
(+) operator,
if we have initially assigned it positively into the operand in the
program.
II. Unary minus (-) Operator
Unary minus operator (-) indicates negative value and differ from the unary plus operator. This (-) operator is also used to perform a type conversion operation on an operand. If a value of the integer operand is positive then that value can be produced as a negatively applying unary minus (-) operator. For example, lets see the expressions shown as:
int x = 0; int y = 25; x = (-y); |
In this expression, a positive value is assigned tothe variable "y". After applying minus plus (-) operator on the operand "y", the value becomes "-25" which indicates it as a negative value. This behavior represents the number in two's complement format.
III. Increment (++) and Decrement Operators
The increment/decrement operators can be a prefix
or a postfix .In a prefix expression (++ x or -- x), an operator is
applied before an operand while in a postfix expression (x ++ or x --) an operator
is applied after an operand. In both conditions 1
is added to the value of the variable and the result is stored
back to the variable. However both operators have
the same effect as "x = x + 1;"
Although there is a major difference between a prefix and a postfix
expressions.
In a prefix expression,
a value is incremented first then this new value is restored back to the
variable. On the other hand, In postfix expression the current value is assigned to a
variable then it is incremented by 1 and restored back to the original variable.
Lets make the things more clear with few examples
( i ) example using prefix unary operator:
public class Prefix{
|
Output of the Program:
C:\nisha>javac Prefix.java C:\nisha>java Prefix The value of x :1 The value of y:1 |
The output of this program shows that always 1 is stored in both variables i.e. the value of "x" is incremented first then it is assigned to the variable "y".
( ii ) example using postfix unary operator:
public class Postfix{
|
Output of the Program:
C:\nisha>java Postfix The value of x :1 The value of y:0 |
The output of the program indicates the value of variable "x" is stored first to the variable "y" then it is incremented by 1.
IV. Logical Compliment (!) Operator
The logical compliment (!) operator is also known as Boolean Negation Operator. It is used to invert the value of a boolean type operand i.e. the type of the operand must be boolean while using this operator,. If the value of the boolean operand is false, the ! operator returns true. But, if the value of the operand is true, the ! operator returns false.
For example, lets see the statements shown as:
boolean result = (2>1); |
In these statements, the first expression returns true to the variable "result" but at the end of the program, the output is shown as false because the compliment (!) operator inverts the value of the variable "result".
Lets have one more example using unary operators in different flavors:
class UnaryOperator {
++number;
|
Output of the Program:
C:\nisha>javac UnaryOperator.java C:\nisha>java UnaryOperator result is now:1 result is now:-1 result is now:0 result is now:1 result of number1 is:0 2 is geater than 1: true 2 is geater than 1: false |