3.8. Operand Evaluation Order

[Page 86 ( continued )]

3.7. Operator Precedence and Associativity

Operator precedence and associativity determine the order in which operators are evaluated. Suppose that you have this expression:

```

3

+

4

*

4

>

5

* (

4

+

3

)

1

```

What is its value? How does the compiler know the execution order of the operators? The expression in the parentheses is evaluated first. (Parentheses can be nested, in which case the expression in the inner parentheses is executed first.) When evaluating an expression without parentheses, the operators are applied according to the precedence rule and the associativity rule. The precedence rule defines precedence for operators, as shown in Table 3.10, which contains the operators you have learned so far. Operators are listed in decreasing order of precedence from top to bottom. Operators with the same precedence appear in the same group . (See Appendix C, "Operator Precedence Chart," for a complete list of Java operators and their precedence.)

Table 3.10. Operator Precedence Chart
Precedence Operator

var++ and var ” ” (Postfix)

+ , - (Unary plus and minus), ++var and ” ”var (Prefix)

(type) (Casting)

! (Not)

* , / , % (Multiplication, division, and remainder)

+ , - (Binary addition and subtraction)

< , <= , > , >= (Comparison)

== , != (Equality)

& (Unconditional AND)

^ (Exclusive OR)

(Unconditional OR)

&& (Conditional AND)

(Conditional OR)

= , += , “= , *= , /= , %= (Assignment operator)

If operators with the same precedence are next to each other, their associativity determines the order of evaluation. All binary operators except assignment operators are left-associative . For example, since + and - are of the same precedence and are left-associative, the expression

[Page 87]

Assignment operators are right-associative . Therefore, the expression

Suppose a , b , and c are 1 before the assignment; after the whole expression is evaluated, a becomes 6 , b becomes 6 , and c becomes 5 . Note that left associativity for the assignment operator would not make sense.

Applying the operator precedence and associativity rule, the expression 3 + 4 * 4 > 5 * (4 + 3) - 1 is evaluated as follows :

Tip

 You can use parentheses to force an evaluation order as well as to make a program easy to read. Use of redundant parentheses does not slow down the execution of the expression.

[Page 87 ( continued )]

3.8. Operand Evaluation Order

The precedence and associativity rules specify the order of the operators but not the order in which the operands of a binary operator are evaluated. Operands are evaluated strictly from left to right in Java. The left-hand operand of a binary operator is evaluated before any part of the right-hand operand is evaluated. This rule takes precedence over any other rules that govern expressions. Consider this expression:

```a + b * (c +

10

* d) / e
```

a , b , c , d , and e are evaluated in this order. If no operands have side effects that change the value of a variable, the order of operand evaluation is irrelevant. Interesting cases arise when operands do have a side effect. For example, x becomes 1 in the following code because a is evaluated to 0 before ++a is evaluated to 1 .

```

int

a =

;

int

x = a + (++a);
```

But x becomes 2 in the following code because ++a is evaluated to 1 , and then a is evaluated to 1 .

```

int

a =

;

int

x = ++a + a;
```

The order for evaluating operands takes precedence over the operator precedence rule. In the former case, (++a) has higher precedence than addition ( + ), but since a is a left-hand operand of the addition ( + ), it is evaluated before any part of its right-hand operand (e.g., ++a in this case).

[Page 88]

In summary, the rule of evaluating an expression is:

• Rule 1: Evaluate whatever subexpressions you can possibly evaluate from left to right.

• Rule 2: The operators are applied according to their precedence, as shown in Table 3.10.

• Rule 3: The associativity rule applies for two operators next to each other with the same precedence.

Applying the rule, the expression 3 + 4 * 4 > 5 * (4 + 3) - 1 is evaluated as follows :

The result happens to be the same as applying Rule 2 and Rule 3 without applying Rule 1. In fact, Rule 1 is not necessary if no operands have side effects that change the value of a variable in an expression.