繁体   English   中英

在下列情况下是否可能发生短路评估

[英]Is it possible short circuit evaluation happens in the following case

public class JavaApplication11 {

    static boolean fun() {
        System.out.println("fun");
        return true;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        boolean status = false;
        status = status & fun();
        status = fun() & status;
    }
}

请问Ja​​va,因为status已经是假的,它会不会执行fun方法? 我测试过,在这两种情况下, fun都会被执行。 但是,它是否符合Java规范?

此处不会发生短路评估,因为您使用的是按位和操作( & )而不是逻辑和操作( && )。

double &用于短路操作。 采用

status = status && fun();

&&运算符保证短路。
&运营商保证不会。

暂无
暂无

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

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