Actually, the Java® way to do it is go from left to right. You first evaluateSince the assignment operator has a low precedence, everything to its right is calculated before the assignment.
Next:-which reduces toThe * operator to the right has a higher precedence, sois calculated, as,which becomesFilling in the ... gives usThe expression now becomes 14 and that is assigned to x.
If you add (), that only changes the order of evaluation when the first ( is reachedNext:-So far, the same as before. When the () appear, however, the expression to the right of is evaluated as a whole expression.
The second () cause their contents to be evaluated, so 3 + 5 becomes 8:-which becomesthen,thenandNo, I can't see anywhere that 10 + 3 appears. Please supply more information about the 10 + 3.
When I wrote ... I meant that part of the expression is only evaluated later, not that it has vanished.
The multiplicative operators (*, /, %) have a higher order of precedence
than the additive operators (+, -). That means when you see an expression such as this:
you first evaluate the 2 * 5 and 3 * 4, which reduces the expression to the following:
Then, you evaluate the remaining terms in left-to-right order, resulting in a value of x of
14. Make sure you understand why the result is 24 as you’ll likely see this kind of operator
precedence question on the exam.
Notice that we said “Unless overridden with parentheses…” prior to Table 2.1. That’s
because you can change the order of operation explicitly by wrapping parentheses around
the sections you want evaluated fi rst. Compare the previous example with the following
one containing the same values and operators, in the same order, but with two sets of
This time you would evaluate the addition operator 10 + 3, which reduces the expression to the following:
You can further reduce this expression by multiplying the fi rst two values within the
Next, you subtract the values within the parentheses before applying terms outside the
Finally, you would multiply the result by 2, resulting in a value of 48 for x