English

Explain the following operator with an example program: Unary Operator

Advertisements

Question

Explain the following operator with an example program:

Unary Operator

Code Writing
Explain
Advertisements

Solution

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
shaalaa.com
  Is there an error in this question or solution?
Chapter 6: Variables and Expressions - EXERCISE [Page 132]

APPEARS IN

Dheeraj Mehrotra Computer Science [English] Class 12
Chapter 6 Variables and Expressions
EXERCISE | Q 31. (f) | Page 132
Advertisements
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×