Hi Everyone,
Not sure if I'm allowed to post book excerpts, but here goes. On page 321 of the
SCJP book by Bert Bates/Kathy Sierra (great book by the way... highly reccomended), I read this:
======== start
which prints
true
You can read the preceding code as, "If both (x > 3) and (y < 2) are true, or if the result of doStuff() is true, then print true." So basically, if just doStuff() alone is true, we'll still get true. If doStuff() is false, though, then both (x > 3) and (y < 2) will have to be true in order to print true. The preceding code is even more complex if you leave off one set of parentheses as follows,
which now prints�nothing! Because the preceding code (with one less set of
parentheses) evaluates as though you were saying, "If (x > 3) is true, and either (y < 2) or the result of doStuff() is true, then print true." So if (x > 3) is not true, no point in looking at the rest of the expression." Because of the short-circuit &&, the expression is evaluated as though there were parentheses around (y < 2) | doStuff(). In other words, it is evaluated as a single expression before the && and a single expression after the &&.
======== end
Now, here is my problem. What does this have to do with short circuit evaluation? Is this not simply a case of operator precedence? Wouldnt the code:
just be evaluated as:
because the | ranks before && in the precedence ladder? There must be something I'm missing here. If it is a case of precedence, what is it doing in the book? Thanks again for helping my confused little brain :-).
- Yeuker