Originally posted by vidya sagar:
When JVM interprets this line by seeing logical OR(|) it assumes left side of the OR operator as one set and right side of the operator as another set
so it evaluates in this fashion
( (b[0] && b[1] ) | b[2] )
That's actually not correct.
If that were true, then the left side of | would be evaluated, and then the right side of | would be evaluated, since | always evaluates both sides. However, this program demonstrates that that is not what is happening (see Case 4):
The output is:
a1=1, b1=0, c1=0, true, Case 1: true || true & false
a2=1, b2=0, c2=0, true, Case 2: true || (true & false)
a3=1, b3=0, c3=1, false, Case 3: (true || true) & false
a4=1, b4=0, c4=0, false, Case 4: false && true | true
a5=1, b5=0, c5=0, false, Case 5: false && (true | true)
a6=1, b6=0, c6=1, true, Case 6: (false && true) | true
The results of case 1 and case 2 are identical, and the results of case 4 and case 5 are identical. After a short-circuit operator, the remaining parts of the expression are not evaluated at all, as shown by the fact that the b and c variables in those cases were not incremented.
In case 4, there is an expression
++a4==9 && ++b4==1 | ++c4==1
in which neither
++b4==1 nor
++c4==1 gets evaluated, despite the fact that the | operator always evaluates both sides before returning a result. This shows that evaluation runs left-to-right until the && short-circuit operator determines that no more evaluation is necessary (because the left side of && was false). The rest of the expression is ignored, and false is returned.