Hands On Java Lab 3 Experiment 6 Experiment 6: Operator Precedence The arithmetic operations of addition, subtraction, multiplication and division are familiar ones. However, when these operations are mixed within an expression, some mechanism must be used to determine which operation is to be performed first. This is important because the value of the expression can change, based on the order in which operators are applied. To illustrate, consider the expression:
16 + 8 / 4
If the operations are simply performed from left-to-right (+, then /), the expression evaluates to
(16 + 8) / 4 = 24 / 4 = 6
but if the division operation for some reason is treated as "more important" and performed before the addition operation, then the expression evaluates to
16 + (8 / 4) = 16 + 2 = 18
The result of an expression thus depends on the order in which operators are applied. Using your source program, find out which operator is applied first in the preceding expression, and report it below, before continuing: The order in which operators are applied is determined by a characteristic called operator precedence. Each Java operator is given a precedence or priority level, and operators with higher priority levels are applied before those with lower priority levels. In Java, the precedence of the operators we have seen thus far are as follows: Higher: () (method call or type construction), + (positive), - (negative), ! (logical NOT) * (multiplication), / (division), % (modulus) + (addition), - (subtraction) < (less-than), <= (less-than-or-equal-to), > (greater-than) >= (greater-than-or-equal-to) == (equality), != (inequality) && (logical AND), Lower: || (logical OR), As indicated, parentheses can be used to change the order in which operators are evaluated, with the operator in the innermost parentheses being applied first. For example, in the expression
((9-1) * (1+1))
the subtraction is performed first, then the addition, and finally the multiplication. Using "pencil and paper," find the value of the following expression:
1 / 2 * (3 - 4) * 5 + (6 % 7 - (8 - 9) / 8) Then use your source program to check your answer. Operator precedence is the reason why it is necessary to parenthesize a relational expression within an output statement. If we were to write:
theScreen.println("\n" + i == j + " done ");
theScreen.println("\n" + i == j );
In the first line, the plus operator has higher precedence than the equality operator, so the value of i would be concatenated onto the string "\n", then the value of j would be concatenated with "done" and finally the two strings would be compared to see if they were the same object. They never will be, so the result will always be false. In the second line, the plus operator has higher precedence than the equality operator, so the value of i would be concatenated onto the string "\n", then equality operator would be applied the resulting string and j. But j is a integer value and since a string and an integer cannot be compared, the compiler generates an error. Using Express.java, type in these (unparenthesized) output statements and in the space below, record (i) whether the error occurs at compile-time or at run-time, and (ii) the error message: Remove the line that generates the error and run the program again. Record the output here: If we instead parenthesize the expression:
theScreen.println("\n" + (i == j) + " done ");
then the equality operator is applied first, producing a boolean value, then the leftmost plus operator is applied, concatenating that boolean value onto the string. The rightmost plus operator is then applied, concatenating "done" . We'll see why in the next experiment. You should now understand what operator precedence is, and how Java uses it to determine the order in which operators are applied. Back to the Exercise List Forward to the Next Experiment Back to the Table of Contents Back to the Introduction Copyright 2000 by Prentice Hall. All rights reserved.