Short Circuit AND, OR and Precedence Table

Hello i have a question regarding precedence Table in java. It says && has a higher precedence over ||.

boolean b,c,d;
b = c = d = false;
boolean e = (b = true) || (c = true) && (d = true);
System.out.println(b+" "+c+" "+d);

When i run mentioned code code it output "true false false". why doesn't it evaluate c = true && d = true part first since && has a higher precedence? Thanks in advance

Jon Skeet
people
quotationmark

The precedence here just means that

X || Y && Z

is equivalent to:

X || (Y && Z)

That executes as:

  • Evaluate X
  • If X is true, the result is true
  • Otherwise, evaluate Y
    • If Y is false, the result of Y && Z is false, so the overall result is false
    • Otherwise, evaluate Z
    • If the result is true, then Y && Z is true, so the overall result is true
    • If the result is false, then Y && Z is false, so the overall result is `false

people

See more on this question at Stackoverflow