[英]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;
}
}
请问Java,因为status
已经是假的,它会不会执行fun
方法? 我测试过,在这两种情况下, fun
都会被执行。 但是,它是否符合Java规范?
此处不会发生短路评估,因为您使用的是按位和操作( &
)而不是逻辑和操作( &&
)。
double &
用于短路操作。 采用
status = status && fun();
&&
运算符保证短路。
&
运营商保证不会。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.