Operator precedence and Operator associativity

  • Review of the operators we have learned so far:

    • Arithmetic operators:   +, -, *, /, %
    • Relational operators:     ==, !=, <, <=, >, <=
    • Logical operators:          !, &&, ||, ^

  • Each operator has:

    1. a precedence (or priority)

    2. an associativity:   either left ⇒ right   or   right ⇒ left

  • Rules to apply/execute operators:

      1. Operators with higher precedence are executed first

      2. Operators with the same precedence are executed according to their associativity

Precedence and associativity of all Java operators (Table 3.8)

  Precedence   Operator                                                Associativity
 ----------------------------------------------------------------------------------- 
    1 (high)   var++   var--                                           left → right
    2	       +  −  (unary plus and minus)   ++var  --var             left → right
    3          (double) (float) (long) (int) (short) (byte)            left → right
    4	       !                      (not)                            left → right
    5	       *    /    %            (multiply, divide, remainder)    left → right
    6	       +    -                 (add, subtract)                  left → right
    7          <   <=    >   >=                                        left → right
    8          ==  !=						       left → right
    9          ^						       left → right
   10	       &&						       left → right
   11	       ||						       left → right
   12          ?:   (conditional expression)                           left → right
   13	       =   +=  -=  *=  /=  %= (Assignment operators)           right → left


Example operator associativity: a - b + c - d = ((a - b) + c) - d // Do same priority operations // from left to right

Do not memorize the precedence of the operators
Use brackets ( ... ) to make sure an expression is evaluated correctly

Quiz

  Precedence   Operator                                                Associativity
 ----------------------------------------------------------------------------------- 
    1 (high)   var++   var--                                           left → right
    2	       +  −  (unary plus and minus)   ++var  --var             left → right
    3          (double) (float) (long) (int) (short) (byte)            left → right
    4	       !                      (not)                            left → right
    5	       *    /    %            (multiply, divide, remainder)    left → right
    6	       +    -                 (add, subtract)                  left → right
    7          <   <=    >   >=                                        left → right
    8          ==  !=						       left → right
    9          ^						       left → right
   10	       &&						       left → right
   11	       ||						       left → right
   12          ?:   (conditional expression)                           left → right
   13	       =   +=  -=  *=  /=  %= (Assignment operators)           right → left


What is printed by this program: double a; boolean b; b = (a = 5 % 3 + 1.5) < 4 - 5 / 2 ; System.out.println( a ); System.out.println( b );

DEMO: demo/03-selections/10-precedence+assoc/Quiz.java