Page 103 - CA_Blue( J )_Class9
P. 103

The ! Operator
                 This operator is known as “NOT” or “Logical NOT” operator.  It is a unary operator because it works only on one
                 operand. The outcome of this operator is false, if the condition is satisfied and if the condition does not satisfy,
                 then the outcome of the operator is true. In other words, it reverses the output of the condition. For example, if
                 we take the expression !(a>b), then the output will be:


                                   Condition 1             Condition 2       !(Condition 1 && Condition 2)
                                      (a>b)                   (b>c)                !((a>b)&&(b>c))

                                     0 / false               1 / true                  1 / true

                                     1 / true               0 / false                  1 / true




                          Note: Relational expression works before the logical part, this is because the precedence of a
                          Relational Operator is higher than Logical Operator.
                          Java evaluates logical operators based on the following order of precedence:
                             NOT (!) – highest precedence

                             AND (&&)
                             OR (||) – lowest precedence
                          This means that in complex expressions, the ! operator will be evaluated first, followed by &&,
                          and finally ||.





                  Program 18     Write a program to demonstrate the use of logical operators.

                   1  public class logical_operator

                   2  {
                   3     public static void main()
                   4      {

                   5        boolean a = true;

                   6        boolean b = false;
                   7
                   8        System.out.println("a && b = " + (a&&b));

                   9        System.out.println("a || b = " + (a||b) );
                  10        System.out.println("!(a && b) = " + !(a && b));

                  11     }
                  12  }











                                                                                               Operators in Java   101
   98   99   100   101   102   103   104   105   106   107   108