• Post Reply Bookmark Topic Watch Topic
  • New Topic

Precedence difference between | and ||  RSS feed

 
Daniel McCracken
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sierra and Bates, in "Sun Certified Programmer & Developer For Java 2, Exam 310-035; 310-037" pp 215-216, have an example that taught me something I didn't know: | and || don't have the same priority, nor do && and &.
I love the book! Not carping. Just wondering if another word of explanation might be needed, and curious why Sun did it that way.
The example says: assume doStuff() returns true. Then this code will print true:
int y = 5;
int x = 2;
if ((((x > 3) && (y < 2)) | doStuff()) {
System.out.println("true");
}

No problem. That's what OR means. But then take out of one set of parens:
int y = 5;
int x = 2;
if ((x > 3) && (y < 2) | doStuff()) {
System.out.println("true");
}

This prints nothing, because the effect is as if there were another set of parens:
if ((x > 3) && ((y < 2) | doStuff())) {

The reason: | has higher precedence than &&. I didn't know that! I stared at that a long time. Doesn't AND have higher precedence than OR??? Well, yes, if you're talking about | and &, or about || and &&.
I figured it out, but a real newbie might appreciate another word of explanation.
So my question: why? Got an example where it's useful that | has higher precedence than ||?
Thanks.
Dan McCracken
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bitwise operations always have a higher precident because they're a lot closer to mathematical operations like mod or +, then they are shortcircuit operations like && or ||. Think of them as mathematical operations that had the misfortune of looking exactly like more complex operations.

As far as reasoning why it is, in some ways that is arbitrary. Java could have been written to give equal or less precident to these operations, especially because they are rarely used (especially in conjunction with ||/&&, that would be a tad confusion).

I would venture to guess it was chosen to be this way for two reasons. One, they are mathematical in nature (I almost read them as being like a multiply operate) and therefore the operands in the equation are tightly tied together. Second, I would think while coding you'd want them to happen first, since the rule of them is that they guarentee both right and left are always performed. Think of it like this:

a & b || c & d || e & f

When I read that line, its much simpler to me to transfer that as "I really want both ab, or both cd, or both ef. Trying to read it otherwise could give you a headache (I want a and... b or c... and d or e.. etc).

In general, file & and | as numerical operation as far as order of precidence. Beside, & and | can be applied to non-booleans whereas && and || must use booleans.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My short lesson from this discussion (over the years) has been, don't rely on my understanding of the order of precedence, add parens, write two lines instead of one or whatever it takes to be clear and explicit.
 
Daniel McCracken
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, friends.
I am left with the feeling that these operators work the way they do because that's how Sun wanted them to work. Live with it. Get over it. OK; I can do that.
I totally agree with the advice, "When in doubt parenthesize." I beat that into my students.
I might note, however, that that attitude will not get you very far on the SCJP.
Finally, new question: how do you say the names of these things? I've got a program projected on the screen and I'm reading it aloud. The chart I have lists | and || both as "Boolean (logical) OR." And | is listed both as "Boolean (logical) OR" and "Bitwise OR." Fine, that distinction is settled by context.
Not to prolong a discussion of no great consequence. Thanks for the responses.
Dan McCracken
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!