No, the way the expression is evaluated goes from left to right, while taking operator precedence into account. So once in a && b
the a
gets false, b
is not evaluated anymore.
JLS §15.23. Conditional-And Operator && says:
The conditional-and operator &&
is like &
(§15.22.2), but evaluates its right-hand operand only if the value of its left-hand operand is true.
Here is a detailed step by step evaluation of how this works:
This is because, when you parse an expression as a human being, you start with the lowest precedence operators first. In this case &&
has lower precedence then ++
.
++a < 0 && foo
^^
start
In order to compute the result of &&
, you should know the left operand:
++a < 0 && ++b
^^^^^^^
so compute this
Now, when you want to know the result of ++a < 0
, take a look at the lowest precedence operator first, which is <
:
++a < 0
^
evaluate this
In order to do that, you will need both left and right operand. So compute them:
++a
0
This is the moment were ++a
gets increased.
So, now we are at the bottom. Let's make our way back to the top:
false && foo
And this is where the fact that && is short circuit comes in. Left operand is false, so the right operand expression foo
is not evaluated anymore. Which becomes:
false
without the evaluation of the right operand.