djrice
djrice

Reputation: 9

short circuit in java

I thought I understood that using short circuit operators that the order of precedence is important however I am having difficulty in understanding why the following code occurs:

line 3. false && true || true   // this returns true
line 4. false && true |  true  //  this returns false

I am correct in stating that the code on line 4 will return false because the evaluation is from right to left. However if line 3 has the left to right evaluation, why does it return a true? Using just two short circuit operators is fine but using three, I am somewhat stuck on the logic. No pun intended.

Upvotes: 0

Views: 351

Answers (1)

nwatx
nwatx

Reputation: 118

Remember operator precedence in Java. | is evaluated before &&, but || is evaluated after &&. Therefore, the first one would evaluate as (false && true) || true which would equal true, while the second one would evaluate as false && (true | true), which evaluates to false.

Upvotes: 4

Related Questions