4

I have got confused due to what is true regarding the operator precedence in Java. I read in tutorials a long time ago that AND has a higher priority than OR, which is confirmed by the answers provided in the question. However, I am currently studying Java using the "Sun Certified Programmer for Java 6 Study Guide". This book contains the following example:

int y = 5;
int x = 2;
if ((x > 3) && (y < 2) | doStuff()) {
    System.out.println("true");
}

I am copying and citing the explanation of how the compiler handles the above code:

If (x > 3) is true, and either (y < 2) or the result of doStuff() is true, then print "true". 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 &&.

This implies though that | has higher precedence than &&. Is it that due to the use of the "non-short-circuit OR" and instead of the short circuit OR? What is true?

9
  • Probably a misprint in the book. A bitwise or b/w (y<2) and doStuff() doesn't make sense. Besides, the explanation talks about "either y<2 is true or doStuff() is true", which is logical OR. Commented Dec 27, 2012 at 17:25
  • probably it's not bitwise OR, check it once again. Commented Dec 27, 2012 at 17:27
  • 2
    | isn't the same as ||. One is bitwise. The other is logical. Commented Dec 27, 2012 at 17:28
  • Ok, false name, not "bitwise or" but "non-short-circuit OR". Commented Dec 27, 2012 at 17:30
  • 1
    Simply for easier understanding I prefer to make some more brackets even if not necessary. Commented Dec 27, 2012 at 17:49

2 Answers 2

8

That's because it is using the | operator instead of ||, which has a higher priority. Here's the table.

Use the || operator instead and it'll do what you think.

Sign up to request clarification or add additional context in comments.

5 Comments

That said, the explanation of the book is completely wrong. The short-circuitness of the && operator doen't have anything to do with why (y < 2) | doStuff() is evaluated as if there were parenthese around it.
@JB Nizet: The example was set on operators precedence not on how the && actually works. Surprisingly the book lacks a table presenting the whole precedence of the operators in this section, therefore got me confused according to my previous knowledge.
Yeah I agree it's a goofy example. Bottom line is the operator precedence table tells all
@arjacsoh: what I mean is that this sentence is wrong: Because of the short-circuit &&, the expression is evaluated as though there were parentheses around (y < 2) | doStuff(). It should in fact be: Because | has a higher precedence than &&, the expression is evaluated as though there were parentheses around (y < 2) | doStuff().
Having a hard time seeing how this even compiles - does the boolean (y < 2) get auto-promoted to int?
0

Because of the short-circuit &&, the expression is evaluated as though there were parentheses around (y < 2) | doStuff() ...

This statement is incorrect, indeed meaningless. The fact that && is a short-circuit operator has nothing to do with the evaluation of (y < 2) | doStuff(), and indeed that could only compile if doStuff() returned a Boolean. What makes the difference (the implicit parentheses) is the precedence of && relative to |, which is defined in JLS ##15.22-23 as && being lower.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.