Here is a question from Dan's mock
test class EBH202 {
static boolean a, b, c;
public static void main (
String[] args) {
boolean x = (a = true) || (b = true) && (c = true);
System.out.print(a + "," + b + "," + c);
}}
What is the output?
The ans is: true,false,false
I understand the && and || are short-circuited operators, but isn't && has higher precedence over ||
Then how come variable a gets value true and not b and c?
My ans was: true,true,true
Reasoning:
&& has higher precedence, so (b=true) is evaluated first. b becomes true.
Since the first part is true, the second part (c=true) is evaluated as well. c becomes true.
Here i get a little confused - the second part of || is already evaluated! Anyway, i am assuming the first part of || also gets evaluated as well.
If you notice I am evaluating this expr the same way i would with an expr like x = a + b * c where the multiplication happens first and then the addition. Where did I go wrong?
Really appreciate your help.