1) a = true is evaluated, so a = true 2) b = true is not evaluated, so b = false (short-circuit) 3) the original expresion would be, boolean x = true && (c = true) 4) c = true is evaluated, so c = true 5) boolean x = true
But the answer is, a = true, b = false, c = false Any ideas? Thanks a lot.
Joined: Jul 22, 2004
It actually works this way.
op1 || op2 where op1, op2 are two operands.
if op1 is "true" then op2 will not be evaluated, EVEN IF op2 is an expression (in this case (b=true) && (c=true)). Similarly for &&, if op1 is false then op2 is not evaluated at all.
One more point to note is both ||, && have same level of precedence and so if they appear in a expression, they will be evaluated as usually (from left-right).
Here it will be evaluated as (false || (true && (true || false))) and so will print "check precedence".
Hope that was helpfull, Kits
Joined: May 01, 2003
If you look at operator precedence order of || and &&, && has the higher precedence than operator ||. And also if you remember java always evaluates left oprand first for binary operators. So the expression will get evaluated as -
boolean x = (a = true) || (b = true) && (c = true);//Original expression.
x = ((a = true) || ((b = true) && (c = true)));//According to the precedence.