logical OR operator vs bitwise OR operator
does anyone know why:
if (false && true || true) {
System.out.println("True");
} else {
System.out.println("False");
}
Print "True"
if (false && true | true) {
System.out.println("True");
} else {
System.out.println("False开发者_运维知识库");
}
Print "False"
In the first case && has higher precedence than || operator so the expression is evaluated as if ( (false && true) || true )
and you get True.
In the second case bitwise OR operator has higher precedence than && so the expression is evaluated as if ( false && ( true | true ) )
and you get False.
Because of operator precedence. In your first example, the &&
is done first, and then the ||
. But the bitwise OR has higher precedence, so in your second example the |
is done first, then the &&
.
精彩评论