[英]JAVA: Why is eclipse calling my second else-if statement a "dead code"?
I am writing a code in JAVA on Eclipse IDE, and I am having a problem with my if-else statement.我正在 Eclipse IDE 上用 JAVA 编写代码,但我的 if-else 语句有问题。 From my third outer "if", I have two "else-if" blocks, and Eclipse is calling my second "else-if" a dead code and skips it -- I checked it using Eclipse debugger, but I don't know why.
从我的第三个外部“if”开始,我有两个“else-if”块,而 Eclipse 将我的第二个“else-if”称为死代码并跳过它——我使用 Eclipse 调试器检查了它,但我不知道为什么。 Eclipse keeps suggesting me to delete the second else-if statement, which significantly changes my code.
Eclipse 一直建议我删除第二个 else-if 语句,这显着改变了我的代码。
Please let me know if there is any other information that I can add, and thanks in advance for all the help!请让我知道是否有任何其他信息可以添加,并在此先感谢所有帮助!
Here is the code:这是代码:
public static void eachCycleUni (Queue processes, int numProcesses, Process[] allProcesses, Process[] original) {
Queue<Process> readyProcesses = new LinkedList<Process>(); //Stores processes that are ready
for (int p = 0; p < allProcesses.length; p++) {
Process currentProcess = allProcesses[p];
while (terminatedProcesses != numProcesses) {
//Nothing --> Nothing or Nothing --> currentRunning
if (currentRunning == null && currentBlocked == null) {
....
}
//currentRunning --> --, or currentRunning --> currentBlocked, or currentRunning --> terminated
else if (currentRunning.CPUburstRemaining != 0 && currentBlocked == null) {
...
}
//THIS ELSE-IF
else if (currentBlocked.IOburstRemaining != 0 && currentRunning == null) {
...
}
cycleNumber++;
}
}
}
The relevant if and else-if checks are:相关的 if 和 else-if 检查是:
if (currentRunning == null && currentBlocked == null) {
...
}
else if (currentRunning.CPUburstRemaining != 0 && currentBlocked == null) {
...
}
else if (currentBlocked.IOburstRemaining != 0 && currentRunning == null) {
...
}
Your first if-checks will check that both currentRunning
and currentBlocked
are both null
.您的第一个 if 检查将检查
currentRunning
和currentBlocked
是否都为null
。
If this is not the case, you will check if currentBlocked
is null
and using currentRunning.CPUburstRemaining
implicitly means currentRunning
cannot be null
(otherwise a NullPointerException
would have been given).如果不是这种情况,您将检查
currentBlocked
是否为null
并隐式使用currentRunning.CPUburstRemaining
意味着currentRunning
不能为null
(否则将给出NullPointerException
)。
After these two checks we therefore know that currentRunning
can never be null
anymore, because it would either have entered the first if
-statement, or would have given a NullPointerException
at the currentRunning.CPUburstRemaining
in the first else if
-block.在这两次检查之后,我们因此知道
currentRunning
永远不会再为null
,因为它要么已经进入第一个if
语句,要么在第一个else if
块中的currentRunning.CPUburstRemaining
处给出NullPointerException
。
Because of that, your && currentRunning == null
part of your else if (currentBlocked.IOburstRemaining != 0 && currentRunning == null)
will always be falsey, so the IDE sees this as dead code.正因为如此,你
&& currentRunning == null
你的一部分, else if (currentBlocked.IOburstRemaining != 0 && currentRunning == null)
永远是falsey,所以IDE认为这是死代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.