Witscale Test Center

9.7 Order of evaluation


9.7 Order of evaluation

When you face a Java expression with multiple operators and operands, you may find it difficult to predict the result. To do it accurately, you need to know the order in which the evaluation takes place. Java has two generic rules while evaluating any Java expression:

1.       Operands are evaluated first before applying any operator and then the expression is evaluated from left to right.

Java operators are left associative which means they evaluate their operands from left to right. In case of binary operators, Java evaluates the left-hand operand before any part of the right-hand operand is evaluated. Figure 9.11 illustrates the order of evaluation of Java expression involving multiple operators. The left-hand operand in multiplication is an assignment expression (a=10).

 

 


Figure 9.11 Order of evaluation (from left to right)

 

Since parentheses have highest precedence, first (a=10) is evaluated. The value of a is now 10 and the assignment returns the assigned value 10. The order of evaluation is from left to right, therefore the next operand evaluated is variable a acting as second operand in multiplication. It will be 10. Now since both operands of multiplication are evaluated, actual multiplication takes place and value of b is 10 * 10 which is 100. Finally this value is assigned to the variable b.

If we remove the round brackets, the above code will be evaluated differently. Figure 9.12 illustrates the step-wise evaluation:

 


Figure 9.12 Order of evaluation and right associativity of assignment operators

 

Note that the order of evaluation of operands is still from left to right. The assignment has the lowest precedence (see table 9.2). Therefore, values are assigned only after all operations are done. Since * operator has higher precedence than assignment operator the 10*2 is evaluated first. Now the assignment expression a = 20 is evaluated as its right side is completely evaluated. It returns 20 which is then assigned to b.

 

2. The evaluation order respects parentheses and operator precedence.

While evaluating from left to right, the operators with higher precedence are evaluated before the operators of lower precedence. In the following code there are two unary operands ++ and -, they have higher precedence on the binary operand +.

 

int a=2, b=6;

int c = a++ + a + -b;

 

Therefore, behind the scene the code is likely to be executed as:

 

 


 

As you can see the evaluation still progresses from left to right but the operands are evaluated before applying any operators. In the first pass, only values of variables are put in. Next, parentheses are searched. Since none is present in this example, next step is to apply the operators one-by-one in order of their precedence. a++ is makes the value of a as 3 but returns 2.Therefore 2++ and -6 are evaluated. Then the fist (from left ) binary + operators is applied. Therefore, the bottom line is that all Java expressions are evaluated from left to right. First, the operands are evaluated. Then the operators are applied as per the precedence.