Explain the following operator with an example program: Unary Operator
Advertisements
प्रश्न
Explain the following operator with an example program:
Unary Operator
कोड लेखन
स्पष्ट कीजिए
Advertisements
उत्तर
A Unary Operator in Java is an operator that performs an operation on a single operand. Unary operators include unary plus (+), unary minus (-), increment (++), decrement (--) and logical NOT (!).
Example:
class UnaryOperator
{
public static void main(String[] args)
{
int a = 10;
boolean b = true;
System.out.println("Unary Plus: " + (+a));
System.out.println("Unary Minus: " + (-a));
System.out.println("Increment: " + (++a));
System.out.println("Decrement: " + (--a));
System.out.println("Logical NOT: " + (!b));
}
}
Output:
Unary Plus:
10 Unary Minus: -10
Increment: 11
Decrement: 10
Logical NOT: false
10 Unary Minus: -10
Increment: 11
Decrement: 10
Logical NOT: false
shaalaa.com
क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
