简体   繁体   English

布尔表达式的编译错误

[英]compilation error on boolean expression

This is a snippet of Java code: 这是一段Java代码:

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. 'bool'变量上的语法错误。 I expect it to print true, false, true . 我希望它能打印true, false, true Although according to my tutorial books it prints true, false, false . 尽管根据我的教程书,它打印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. 那不是家庭作业,我正在学习Java。 Cheers 干杯

(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. 由于(a = true)被评估为true ,因此第二个表达式不被评估,因为您在此处使用了短路运算符(||)

And hence the last two assignment does not happen. 因此,最后两个任务不会发生。 And the values of b and c remain false . 并且bc的值保持为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为假,则a && b不会求值b

  • a || b a || b will not evaluate b , if a is true. 如果a为true,则a || b不会评估b

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 || 这是因为||的LHS is evaluated, setting a to true and evaluating to true . 被评估,将a设置为true,并评估为true As the || 作为|| is short-circuiting, the RHS of || 短路, ||的RHS (which is (b = true) && (c = true) ) isn't evaluated. (即(b = true) && (c = true) )不会被评估。

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 . 如果是后者,您将得到true, false, true

This is because && has higher precedence ("binds tighter than") || 这是因为&&具有更高的优先级(“绑定比”更紧密) || . See the Java tutorial for a complete list of operator precedence. 有关运算符优先级的完整列表,请参见Java教程

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"; 由于您没有使用布尔值初始化,因此将为java分配默认值“ 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". 由于第一个评估返回true(a = true)-> true,因此第二部分未“执行”。

With the operator || 与运营商|| (true || //do not matter) = true. (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. 是一种优化形式,无需计算后半部分,它的第一个已被评估为true。

If I might.... 如果可以的话....

It looks like some people aren't quite getting what the question is. 看起来有些人不太了解问题所在。

x = (a=true) || x =(a = true)|| (b=true) && (c==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. 由于&&的优先级高于||,因此似乎(b = true)&&(c == true)应该首先被求出,从而将b和c设置为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. Rohit Jain到目前为止已经解释得最好了。 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. 首先,通过执行赋值的“副作用”将a = true评估为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) || x =(b = true)&&(c = true)|| (a=true); (a = true);

Of course, then b and c are set to true and a remains false because || 当然,然后将b和c设置为true,而a保留为false,因为|| short circuits. 短路。

I don't think I've explained anything new, just the same info in smaller steps. 我认为我没有解释任何新内容,只是在较小的步骤中提供了相同的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM