This is a snippet of Java code:
static boolean a; // gets false
static boolean b;
static boolean c;
public void printA(){
boolean bool = (a = true) || (b = true) && (c = true);
System.out.print(a + ", " + b + ", " + c);
}
It does not compile, what is the prob? Error: multiple markers on this line; syntax error on the line of ‘bool’ variable.
I expect it to print true, false, true.
Although according to my tutorial books it prints true, false, false.
I understand it performs short-circuiting but in case of && both sides needs to be evaluated. That is not a homework, I am learning Java.
Cheers
is equivalent to: –
Since
(a = true)is evaluated totrue, hence the 2nd expression is not evaluated, since you are using short-circuit operator (||) there.And hence the last two assignment does not happen. And the values of
bandcremainfalse.Note: – Short-circuit operators –
&&and||, does not evaluate further if a certain result can be obtained by previous evaluation.So: –
a && bwill not evaluate b, if a is false.a || bwill not evaluate b, if a is true.