compilation error on boolean expression

喜你入骨 提交于 2019-12-02 10:43:25
(a = true) || (b = true) && (c = true);

is equivalent to: -

(a = true) || ((b = true) && (c = true));

Since (a = true) is evaluated to true, 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 and c remain false.

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.

There's no compilation error for me - this works fine:

public class Test {
    static boolean a;
    static boolean b;
    static boolean c;

    public static void main(String[] args) {
        boolean bool = (a = true) || (b = true) && (c = true);
        System.out.print(a + ", " + b + ", " + c);
    }
}

It prints out

true, false, false

This is because the LHS of the || is evaluated, setting a to true and evaluating to true. As the || is short-circuiting, the RHS of || (which is (b = true) && (c = true)) isn't evaluated.

Note that the expression

(a = true) || (b = true) && (c = true);

is equivalent to:

(a = true) ||  ((b = true) && (c = true))

not

((a = true) || (b = true)) && (c = true)

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.

static boolean a;
static boolean b; 
static boolean c;

Since, you did not initialize with a value your booleans, java will assigned then the default value "false";

The problem is that:

(a = true) || (b = true) && (c = true);  

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.

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.

     ||
   /    \
 =      &&
/ \    /   \
a t    =   =
      / \ / \
      b t c t

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!