As I was reading a colleague's Java code, I stumbled upon an army of if/else statements. In these statements, several &&
and ||
operators were fighting each other without any help from parenthesis. I simplified the statements into:
if (true || true && false)
return true;
else
return false;
What do you think the result would be? Honestly, I thought it would be false
, but it seems short-circuiting doesn't work like I expected. In this case, the result is true
. The short-circuit mechanism seems to consider the whole expression as true
when it finds true
immediately followed by ||
.
But in the reversed expression, what is the result?
if (false && true || true)
return true;
else
return false;
If we follow the same logic, it should be false. the first boolean is false
and it is immediately followed by &&
, but the result is true
, once again. This makes sense to me, but it seems incompatible with our previous experiment.
So here's my theory:
If we find a true
followed by ||
, then it is true
, no matter what might comes next, even if there is a long list of other logical operators coming after. But if we find a false
followed by &&
, it only short-circuits the next element, not the whole statement.
And here's my question:
Am I right? It seems a bit silly to me. Is true
stronger than false
?
&&
has higher precedence than||
as mentioned in existing answers (just wanted to provide the link) – Upholstery