+ Unary plus operator; indicates positive value (numbers are positive without this, however)
- Unary minus operator; negates an expression
++ Increment operator; increments a value by 1
-- Decrement operator; decrements a value by 1
! Logical complement operator; inverts the value of a boolean
The following program, UnaryDemo, tests the unary operators:
class UnaryDemo {
public static void main(String[] args){
int result = +1; // result is now 1
System.out.println(result);
result--; // result is now 0
System.out.println(result);
result++; // result is now 1
System.out.println(result);
result = -result; // result is now -1
System.out.println(result);
boolean success = false;
System.out.println(success); // false
System.out.println(!success); // true
}
}
The increment/decrement operators can be applied before (prefix) or after (postfix) the operand. The code result++; and ++result; will both end in result being incremented by one. The only difference is that the prefix version (++result) evaluates to the incremented value, whereas the postfix version (result++) evaluates to the original value. If you are just performing a simple increment/decrement, it doesn't really matter which version you choose. But if you use this operator in part of a larger expression, the one that you choose may make a significant difference.
The following program, PrePostDemo, illustrates the prefix/postfix unary increment operator:
class PrePostDemo {
public static void main(String[] args){
int i = 3;
i++;
System.out.println(i); // 4"
++i;
System.out.println(i); // "5"
System.out.println(++i); // "6"
System.out.println(i++); // "6"
System.out.println(i); // "7"
}
}
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.
( i ) example using prefix unary operator:
public class Prefix{
public static void main(String[] args){
int x = 0;
int y = 0;
y = ++x;
System.out.println("The value of x :" + x);
System.out.println("The value of y:" + y);
}
}
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{
public static void main(String[] args){
int x = 0;
int y = 0;
y = x++;
System.out.println("The value of x :"+ x );
System.out.println("The value of y:" + y );
}
}
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.
No comments:
Post a Comment