short circuit operators (&& , ||) differs from non-short circuit operators (& , |) in the way they deal with the right hand side expression.

the short circuit && evaluate the expression on the left hand side first, if it is false so there is no reason to evaluate the right hand side expression, because in all cases the final result will be false.

also the short circuit || evaluate the expression in the left hand side first, if it is true so there is no need to evaluate the right hand side because in all case the final result will be true.

so when using short circuit operators (&& , ||) you should expect that the right hand side expression may not be evaluated at all.

the non-short circuit operators (& , |) are less efficient than short circuit operators because they evaluate the right hand side even they knows that the evaluation of it will make no difference to the final result.

here is an example:

int x =3, y=8;

if(x < y || y++ < 9)

{

System.out.println(” the int y will never be incremented”);

}

the part y++ < 9 will not be evaluated so the int y will never be incremented because the left hand side of the && operator will be true so && operator will escape evaluating the right hand side.

### Like this:

Like Loading...

*Related*

## Leave a Reply