1

Hi I have this example:

  (1)
  if(false && false ||true){
            System.out.println("a");
  }

result:

a

and

  (2)
  if(false && false | true){
            System.out.println("a");
  }

result:

(empty)

In case (1) we have short-circuit OR , but in the second we have long-circuit OR. From where comes the difference in the behavior? I know that the expression is evaluated from left to right and AND have bigger priority than OR, so I can't figure out why this happens in case 2?

Xelian
  • 16,680
  • 25
  • 99
  • 152

2 Answers2

5

&& has priority over || but not over |. See operator precedence list.

assylias
  • 321,522
  • 82
  • 660
  • 783
3

(1)

if (false && false || true) {
    System.out.println("a");
}

Here && has higher precedence than ||

  1. false && false gets evaluated first resulting false.
  2. false || true gets evaluated resulting true.

So the result outputs "a".

(2)

if (false && false | true) {
    System.out.println("a");
}

Here | has higher precedence than &&

  1. false | true gets evaluated first resulting true.
  2. false && true gets evaluated resulting false.

So nothing is output.

Hope it makes sense

William Price
  • 4,033
  • 1
  • 35
  • 54
Suvasis
  • 1,451
  • 4
  • 24
  • 42