Page 109 - CA_Blue( J )_Class9
P. 109

12.        logical OR                        ||
                         13.        conditional (ternary)             ? :
                         14.        assignment including shorthand    =, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=, >>>=


                     5.6 ASSOCIATIVITY OF OPERATORS
                 If an expression contains two or more operators with the same precedence, then Java compiler executes the
                 expression according to the associativity of the operators. Associativity informs the compiler about the direction
                 from which the operators will be executed. If some operators have the same hierarchy then the calculation is
                 carried out according to their associativity, which is either from left to right or right to left.

                 Suppose, in an expression “a+b-c” the + and – operators are used without any parenthesis, since the binary
                 operators have left to right associativity, the addition will be executed before the subtraction.

                 Similarly, in the expression “a%b*c” the % and * operators are used without any parenthesis, since the binary
                 operators has left to right associativity, the modulus will be executed before the multiplication.
                 Some important associativities are as follows:

                                               Operators                      Associativity
                                   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
                                   ternary                                    Right to Left

                 Some more solved examples of Operators:

                 Arithmetic expressions:
                 1.     if int m=20, n;                                         [shows the difference prefix and postfix]

                             n  = ++m + m;
                                = 21 + 21
                                = 42
                             n  = m++ + m;
                                = 20 + 21
                                = 41
                             n  = m + m++
                                = 20 + 20
                                = 40
                             n  = m + ++m

                                = 20 + 21
                                = 41
                 2.     if a=100, b=10, c;                                 [if datatype is not provided, then default is int]
                             c  = a++ + ++a / b
                                = 100 + 102/10                                              [since int/int will return int]
                                = 100 + 10
                                = 110


                                                                                               Operators in Java   107
   104   105   106   107   108   109   110   111   112   113   114