Please don't look at the condition as they are here to ease the understanding of the behavior
Why does result equals true ?
boolean result = false && (false)?false:true;
I know we can solve the issue doing:
boolean result = false && (false?false:true);
But I am just wondering why the first syntax is incorrect, looks like the '?' operator has more priority over '&&'
The ternary conditional ( ?:
) has lower precedence than &&
. So
boolean result = false && (false)?false:true;
(having unnecessary parentheses); is equivalent to
boolean result = (false && false) ? false : true;
Since (since false && false
is false
), this reduces to
boolean result = false ? false : true;
which, of course, is true
.