开发者

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 &&.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜