[英]Why doesn't this if-statement short circuit?
I'm currently fixing a bug in someone else's Java code, but I cannot explain the bug. 我目前正在修复他人Java代码中的错误,但无法解释该错误。 The code in question is the following if-statement:
有问题的代码是以下if语句:
if (locked && DEBUG_ENABLED
&& owner != null
&& (owner.equals(playerName) || subowner.equals(playerName))
&& handleCommand(playerName, message)) {
....
} else {
....
}
In which DEBUG_ENABLED
is initialized as private static boolean DEBUG_ENABLED = false;
其中
DEBUG_ENABLED
初始化为private static boolean DEBUG_ENABLED = false;
and handleCommand
functions like this: 和
handleCommand
函数是这样的:
public boolean handleCommand(String name, String msg) {
if(msg.equals("Command1")) {
....
} else if(msg.equals("Command2")) {
....
} ....
} else { // No matching command
return false;
}
return true;
}
What puzzles me is that even though DEBUG_ENABLED
is set to false, the code still calls and executes the handleCommand
function. 使我感到困惑的是,即使将
DEBUG_ENABLED
设置为false,代码仍然会调用并执行handleCommand
函数。 I always thought this wasn't supposed to happen due to short circuiting. 我一直认为这不应该由于短路而发生。 The if-statement itself in total is still evaluated as false, since only the code inside the else-block in the first snippet is executed.
由于仅执行第一个片段中else块内的代码,因此if语句本身总共仍被评估为false。
So, how come this if-statement is behaving like this? 那么,这个if语句的表现如何呢? Is it failing to short-circuit, or do I misunderstand the principle, or is there something completely different wrong with this part of code?
是不是短路,还是我误解了原理,或者这部分代码有完全不同的错误? (Besides the missing null check for
subowner
that is, which is done outside of this part.) (除了缺少子
subowner
空检查之外,这是在此部分之外完成的。)
It is not possible that the &&
operator fails to short-circuit. &&
操作符不可能无法短路。 Were you using &
perhaps? 您是否正在使用
&
? If not it means you have made some false assumptions that previous conditions before the last one were false. 如果不是,则意味着您做出了一些错误的假设,即在最后一个条件之前的先前条件是错误的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.