compilation error on boolean expression

前端 未结 4 570
春和景丽
春和景丽 2021-01-25 23:56

This is a snippet of Java code:

static boolean a; // gets false
static boolean b; 
static boolean c;

    public void printA(){

         boolean bool = (a = tru         


        
相关标签:
4条回答
  • 2021-01-26 00:10

    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.

    0 讨论(0)
  • 2021-01-26 00:11
    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.

    0 讨论(0)
  • 2021-01-26 00:22

    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.

    0 讨论(0)
  • 2021-01-26 00:24
    (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.

    0 讨论(0)
提交回复
热议问题