Page 95 - CA_Blue( J )_Class9
P. 95

Unary Minus (-) Operator
                 The unary minus (-) operator works on a single operand and is used to negate its value. This operator changes the
                 sign of the operand: if the operand is positive, it becomes negative, and if it is negative, it becomes positive. The
                 operand can be an integer or a floating-point (real) literal. For examples:

                        if a=-140     then,  = -(a)  = 140

                        if i= -20.5    then,  = -(i)   = 20.5

                  Program 11     Example of Unary minus (-) operator.

                   1  class unarysubtract

                   2  {
                   3      public static void main(int a)

                   4      {
                   5          int b= -a;

                   6          System.out.println("Operation Unary (-) operator of "+a+" is "+b);
                   7      }

                   8  }

                      Input:                                You will get the following output:















                 Unary Increment (++) Operator
                 The unary increment operator (++) works on a single operand and is used to increase its value by 1. This operator
                 only works on variables (or identifiers) with numeric data types (typically integers, though some languages support
                 it for floating-point numbers). There are two forms of the increment operator:
                 •  Prefix Increment (++i): Increases the value of i by 1 before using it in an expression.
                 •  Postfix Increment (i++): Uses the current value of i in an expression and then increases it by 1 afterward. For
                    example:

                        If i=-20      then,  = ++i   = -19
                        If i= -20.5   then,  = i++   = -20.5


                  Program 12     Example of Unary increment (++) operator.

                   1  class unaryincrement
                   2  {

                   3      public static void main(int a)



                                                                                               Operators in Java   93
   90   91   92   93   94   95   96   97   98   99   100