• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

SCJP Book P 3321 - Short Circuit

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is a combination of both.

Operator precedence tells you which expressions are together, but then depending on the result of the first operand, you may not evaluate the second.
 
Cory Max
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok everyone, still confused on this one. My confusion is this:

I thought we didn't need to know operator precedence for the exam. Without knowing that | is evaluated first, there is no way we could figure out this question using only concepts from short circuits. Correct? WOuld we need to know that | evaluates before &&?

Thanks ranchers,

yeuker
 
reply
    Bookmark Topic Watch Topic
  • New Topic