According to me (and my little program) the eplanation in the book is correct! In
Java, subexpressions are evaluated from left to right (when there is a choice). So, for example in the expression
A() + B() * C(D(), E()), the subexpressions are evaluated in the order
A(),
B(),
D(),
E(), and
C().
C() is evaluated last, because it requires the results from
D() and
E().
Output:
++x 4 | x-- 4 | --x 2 |
2-7
Let's look at another example:
If the post-unary operator is always executed first, both statements should print exactly the same values for both
i and
j. But the output is different (for
j):
4-180
4-80
And it makes sense, because subexpressions are evaluated from left to right, so you'll have
And probably the most obvious example. Let's use a short-circuit operator:
If the post-unary operator is always executed first, this program should print
11-true, but it doesn't: it prints
10-true because the left operand (
i3 < 15) is evaluated first and evaluates to
true. And because
|| is a short-circuit operator, the right operand (
i3++ > 10) is not evaluated (and thus
i3 is not incremented).
Hope it helps!
Kind regards,
Roel