File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools
Big Moose Saloon
 Search | Java FAQ | Recent Topics Register / Login

# Precedence difference between | and ||

Daniel McCracken
Greenhorn

Joined: Jul 09, 2005
Posts: 9
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
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3688

2

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

Joined: Jan 29, 2003
Posts: 8791
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.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Daniel McCracken
Greenhorn

Joined: Jul 09, 2005
Posts: 9
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

I agree. Here's the link: http://aspose.com/file-tools

subject: Precedence difference between | and ||