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?

Community
  • 1
  • 1
arjacsoh
  • 8,932
  • 28
  • 106
  • 166

2 Answers2

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.

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
mprivat
  • 21,582
  • 4
  • 54
  • 64
  • 1
    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 Dec 27 '12 at 17:32
  • @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. – arjacsoh Dec 27 '12 at 17:35
  • Yeah I agree it's a goofy example. Bottom line is the operator precedence table tells all – mprivat Dec 27 '12 at 17:36
  • 2
    @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()*. – JB Nizet Dec 27 '12 at 17:40
  • Having a hard time seeing how this even compiles - does the boolean `(y < 2)` get auto-promoted to int? – Alex Dec 27 '12 at 20:36
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.

user207421
  • 305,947
  • 44
  • 307
  • 483