简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM