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
If I might....
It looks like some people aren't quite getting what the question is.
x = (a=true) || (b=true) && (c==true);
Since && has a higher precedence than ||, it seems like (b=true) && (c==true) should be evaulated first, thus setting b and c to true. If && has higher precedence, why is one of the operands for the || evaluated first?
And Rohit Jain has explained it the best so far. All I might add is that precedence of operators doesn't dictate the order in which the operands are evaluated -- merely what operations must be completed as operands for for other operators if not rendered unnecessary by short-circuit operators. The precedence determines the tree for the expression (with, ironically, higher-precedent operators going lower in the tree), but then the tree is evaluated depth-first and left-to-right, regardless of operators precedence.
First the a=true is evaluated, with the "side effect" of doing the assignment, to a value of true. Since || short circuits, the other side isn't even looked at.
If you really want the && (and its operands) to be evaluated first, you'd have to rewrite the expression:
x = (b=true) && (c=true) || (a=true);
Of course, then b and c are set to true and a remains false because || short circuits.
I don't think I've explained anything new, just the same info in smaller steps.
There's no compilation error for me - this works fine:
It prints out
This is because the LHS of the
||
is evaluated, settinga
to true and evaluating totrue
. As the||
is short-circuiting, the RHS of||
(which is(b = true) && (c = true)
) isn't evaluated.Note that the expression
is equivalent to:
not
If the latter were the case, you'd get
true, false, true
.This is because
&&
has higher precedence ("binds tighter than")||
. See the Java tutorial for a complete list of operator precedence.Since, you did not initialize with a value your booleans, java will assigned then the default value "false";
The problem is that:
since the first evaluation returns true (a = true) -> true the second part is not "executed".
With the operator ||
(true || //do not matter)
= true. Is a form of optimization, no need to compute the second half it the first one is already evaluated as true.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
b
andc
remainfalse
.Note: - Short-circuit operators -
&&
and||
, does not evaluate further if a certain result can be obtained by previous evaluation.So: -
a && b
will not evaluate b, if a is false.a || b
will not evaluate b, if a is true.