1

i have been studying for Oracle java certification exam for a while.

today i came across a question asking which part of expression evaluates.

it was sth like a() || b() && c().

so far everything seems ok. && operator precedence has higher priority then || operator and it evaluates first and then || operator evaluates.

the problem arises when i try to run similar code on my windows 7/java 7 machine. when i run my code the || operator evalautes first. I checked a few books, and the one i study(manning java associate prep.) it says vice versa. what do you think whats wrong?

here is my code you can check

public class Dummy {


public static  void main(String[] arg) {

boolean ax = method1() ||  method2()  &&  method3() ;

}

public static boolean method1(){

System.err.println("method1");

return false;
}

public static boolean method2(){

System.err.println("method2");
return false;
}

public static boolean method3(){
System.err.println("method3");
return true;
}

}

Edit : what i get as output : method1 , method 2 What i expect : method2 , method1

edit 2: this is what the book says (manning OCA java Se 7 programmer 1 cert. guide) page 507

The expression (a >= 99 || a <= 33 && b == 10) has three operands together with the OR(||) and AND(&&) short-circuit operators.

Because the short-circuit operator AND has higher operator precedence than the short-circuit operator OR, the expression is evaluated as follows: (a >= 99 || (a <= 33 && b == 10)) Evaluation of the preceding expression starts with the evaluation of (a <= 33 && b == 10).

Because a <= 33 evaluates to true, the operator && evaluates the second operand (b == 10) to determine whether (a <= 33 && b == 10) will return trueor false. a <= 33returns true and b == 10 returns false, so the expression (a <= 33 && b == 10)returns false. The original expression—(a >= 99 || (a <= 33 && b == 10)) is now reduced to the following expression: (a >= 99 || false)

The short-circuit operator OR(||) executes its first operand (even if the value of the second operand is known), evaluating a >= 99. So for this expression, all three oper-ands are evaluated.....

Lidovic
  • 273
  • 1
  • 3
  • 6

2 Answers2

2

The && operator is handled before the ||, but the operands are evaluated before any operator handling.

Section 15.7.2 of the JLS states:

The Java programming language guarantees that every operand of an operator (except the conditional operators &&, ||, and ? :) appears to be fully evaluated before any part of the operation itself is performed.

So, method1() and method2() are evaluated before || or && operate. The && short-circuits, so method3() is not called. Then && operates, yielding false, then || operates, yielding false.

This is printed:

method1
method2
rgettman
  • 176,041
  • 30
  • 275
  • 357
  • @user3580294 The `||` operator evaluates its operands -- `method1()` and `method2() && method3()`. `method1()` prints `method1` and returns `false`. Then the `&&` operator evaluates its operands -- `method2()` prints `method2` and returns `false`, and `&&` short-circuits and yields `false`. Then the `||` operator operates and yields `false`. – rgettman Jun 11 '14 at 22:10
  • Oh right, seems like I'm getting my precedence mixed up -- thought higher precedence went later for some inexplicable reason. My mistake. – awksp Jun 11 '14 at 22:11
1

No, you just got confused.

They are executed like this.

method1() || (method2() && method3());

Your method1 returns false, it is evaluated first (as we have left to right here). Then method2 is evaluated and returns false. And finally it makes no sense to evaluate method3, the result of the whole && is already known to be false. Finally, false || false gives false. This is the final result and method3 was not even evaluated to reach to this result.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159