No, the way to evaluate an expression goes from left to right, taking into account the priority of the operator. So, once a && b a gets false, b no longer evaluated.
JLS ยง15.23. The conditional and operator & says:
The conditional and operator && is similar to & (ยง15.22.2), but evaluates its right operand only if the value of its left operand is true.
Below is a detailed step-by-step evaluation of how this works:
This is because when you analyze an expression as a person, you start first with the operators with the lowest priority. In this case, && has a lower priority than ++ .
++a < 0 && foo ^^ start
To calculate the && result, you need to know the left operand:
++a < 0 && ++b ^^^^^^^ so compute this
Now, when you want to know the result ++a < 0 , first look at the operator with the lowest priority, which is < :
++a < 0 ^ evaluate this
To do this, you will need both the left and the right operand. So, calculate them:
++a 0
This is the moment when ++a incremented.
So now we are down. Back to the top:
false && foo
And this is where the fact that && is short-circuited. The left operand is false, so the correct expression for the operand foo no longer evaluated. Which becomes:
false
without evaluating the correct operand.
Martijn courteaux
source share