compilation error on boolean expression

2019-03-06 09:52发布

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

4条回答
我命由我不由天
2楼-- · 2019-03-06 10:30

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.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-03-06 10:32

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.

查看更多
Root(大扎)
4楼-- · 2019-03-06 10:34
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.

查看更多
smile是对你的礼貌
5楼-- · 2019-03-06 10:44
(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.

查看更多
登录 后发表回答