| Operator and Expression |
| |
| Operator Precedence |
| |
| Java expressions are typically evaluated from
left to right, there still are many times
when the result of an expression would be indeterminate without other rules. |
| |
| The following expression illustrates the problem: |
| x = 2 * 6 + 16 / 4 |
| |
| |
By using the left-to-right evaluation of the expression, the multiplication operation 2 * 6
is carried out first, which leaves a result of 12. The addition operation 12 + 16 is then
performed, which gives a result of 28. The division operation 28 / 4 is then performed,
which gives a result of 7. Finally, the assignment operation x = 7 is handled, in which the
number 7 is assigned to the variable x.
But--it's wrong! The problem is that using a simple left-to-right evaluation of expressions
can yield inconsistent results, depending on the order of the operators.
The solution to this problem lies in operator precedence, which determines the order in
which operators are evaluated. Every Java operator has an associated precedence.
Following is a list of all the Java operators from highest to lowest precedence.
In this list of operators, all the operators in a particular row have equal precedence.
The precedence level of each row decreases from top to bottom. This means that the []
operator has a higher precedence than the * operator, but the same precedence as the
() operator. |
| |
| |
|
| . |
[] |
() |
|
| ++ |
- |
! |
~ |
| * |
/ |
% |
|
| + |
- |
|
|
| << |
>> |
>>> |
|
| < |
> |
<= |
>= |
| = |
!= |
|
|
| & |
|
|
|
| ^ |
|
|
|
| && |
|
|
|
| || |
|
|
|
| ?: |
|
|
|
| = |
|
|
|
|
| |
| Evaluation of expressions still moves from
left to right, but only when dealing with operators that have the same precedence. |
| |
| Otherwise, operators with a higher precedence are evaluated before
operators with a lower precedence. |
| |
| Knowing this, take another look at the sample equation: |
| x = 2 * 6 + 16 / 4 |
| |
| Before using the left-to-right evaluation
of the expression, first look to see whether any of the operators
have differing precedence. |
| |
Here the multiplication (*) and division (/) operators both
have the highest precedence, followed by the addition operator (+), and then
the assignment operator (=).
Because the multiplication and division operators share the same precedence, evaluate
them from left to right. Doing this, we first perform the multiplication operation 2 * 6
with the result of 12. Then we perform the division operation 16 / 4, which results in 4. |
| |
| After performing these two operations, the expression looks like this: |
| x = 12 + 4; |
| |
| Because the addition operator has a higher
precedence than the assignment operator, we perform the addition
operation 12 + 4 next, resulting in 16. Finally, the assignment
operation x = 16 is processed, resulting in the number 16 being assigned to the variable x. |
| |
| As we can see, evaluating the
expression using operator precedence yields a completely different result. |
| |
| Just to get the point across, take
a look at another expression that uses parentheses for grouping purposes: |
| x = 2 * (11 - 7); |
| |
| Without the grouping parentheses, we would
perform the multiplication operation first and then the subtraction
operation. However, referring back to the precedence list, the ()
operator comes before all other operators. So the subtraction
operation 11 - 7 is performed first, yielding 4 and the following expression: |
| x = 2 * 4; |
| |
| The rest of the expression is easily resolved
with a multiplication operation and an assignment operation
to yield a result of 8 in the variable x. |
| |
| |
|
| |
| |