Page 71 - CA_Blue( J )_Class10
P. 71

Hierarchy Order                    Operators                        Precedence
                                  6            equality, non-equality                    ==, !=
                                  7            logical AND                               &&

                                  8            logical OR                                ||
                                  9            ternary conditional operator              ? :
                                  10           assignment operator                       =, +=, -=, *=, /=, %=

                       Note: Relational expression works before the logical expression because the precedence of a relational
                       operator is higher than the logical operator. In logical operators, the ! (NOT) operator has the highest
                       precedence then comes && (AND) and || (OR) operator respectively.


                 4.4.1 Relational Operator and Logical Operator
                 1.  int a=10,b=2;
                   a.  a>b = 10 > 2 = true
                   b.  a!=b = 10 != 2 = true

                   c.  b==(a*10) = 2 == 100 = false
                   d.  b>a = 2>10 = false
                   e.  (b+a)>=(b*a) = 12>=20 = false

                 2.  int a=10,b=2,c=15;
                   a.  (a>b) || (b==c) = (10>2) || (2==15) =true||false = true

                   b.  ((a+b)>c) || (a>c) = (12>15) || (10>15) =false|| false = false
                   c.  (a>b) && (b==c) = (10>2) && (2==15) =true&&false = false

                   d.  ((a+b)>c) && (a>c)= (12>15) && (10>15) =false&& false= false
                   e.  ! ((a/b)>c) = !((10/2)>15) =!(false) = true
                   f.  !(a!=(c-b)) = !(10!=(15-2)) =!(true) = false

                 4.4.2 Associativity of Operators
                 If an expression contains two or more operators with the same precedence, then the Java compiler executes the
                 expression  according  to the associativity of the operators. Associativity tells the compiler  in  which  direction the
                 operators will execute. If some operators have the same hierarchy then the calculation is done according to their
                 associativity, which is either from left to right or vice versa.
                 Suppose, in the expression "a+b-c", the + and - operators are used without any parenthesis, since the binary operators
                 have left to right associativity, the addition operator will be executed before the subtraction operator.

                 Similarly, in the expression "a%b*c", the % and * operators are used without any parenthesis, since the binary operators
                 have left to right associativity, the modulus operator will be executed before the multiplication operator.

                 Some important associativity rules are as follows:

                                                Operators                  Associativity of the Operators
                                  postfix unary                           Right to Left
                                  unary including prefix, logical NOT     Right to Left

                                  multiplication, division and modulus    Left to Right
                                  addition and subtraction                Left to Right




                                                                                                                       69
                                                                                                      Operators in Java   69
   66   67   68   69   70   71   72   73   74   75   76