Does quot;returnquot; stop the execution of a method?(是否“返回?停止执行方法?)
问题描述
我用以下方式编写了一个方法:
I have programmed a method in the following way:
if (something) {
return 1;
}
the rest of the code
在我看来,该方法返回 1,然后执行 其余代码
.这可能是事实吗?return
不会停止代码的执行.不是,如何强制方法停止?
It seems to me that the method returns 1 and then execute the rest of the code
. Can it be the truth? Doesn't return
stops the execution of the code. It it is not, how can I force a method to stop?
添加
这是代码(根据要求):
Here is the code (as requested):
for (int i=availableTime; i>0; i=i-1) {
final int sec = i;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
String lbl = "<html>";
lbl += "</html>";
timeLeftLabel.setText(lbl);
}
});
try {Thread.sleep(1000);} catch (InterruptedException e) {}
parameterFromClientsListener = clientsListener.getValue(userName,parameterToGet);
if (!parameterFromClientsListener.equals("null")) {
output = parameterFromClientsListener;
game.log.fine(userName + " set (by button) " + parameterToGet + " to be equal to " + output + " . [IMPORTANT]");
return output;
}
}
game.log.fine("The partner selection phase is expired.");
// This code is executed if the Submit button was not pressed and the time run out.
if (parameterToGet.equals("partner")) {
tellMyChoice(parameterToGet, this.partnerFromForm, "timer of" + field);
output = this.partnerFromForm;
}
game.log.fine(parameterToGet + " was submitted by timer (not by OK button).");
} else {
output = parameterFromClientsListener;
}
game.log.fine(userName + " set (by timer)" + parameterToGet + " to be equal to " + output + " . [IMPORTANT]");
return output;
}
我运行此代码两次.在每种情况下,我都会生成一个日志文件.在两个日志文件中,我都看到设置(按按钮)"语句(直接在 return
之前).但问题是在第二个日志文件中我确实看到了timer of"语句.如果达到设置(按按钮)",则不应达到.怎么可能?我需要提一下,设置(按按钮)"和计时器"不会出现在我的代码中的其他任何地方(它们只出现一次).
I run this code two times. In every case I generate a log-file. In both log files I see "set (by button)" statement (which is straight before the return
). But the problem is that in the second log file I do see "timer of" statement. Which should not be reached if the "set (by button)" is reached. How can it be? I need to mention that "set (by button)" and "timer of" do not occur anywhere else in my code (they occur only once).
添加 3 个
从代码中可以看出,我没有 finally
语句.
As you can see from the code I do not have the finally
statement.
推荐答案
这不是真的,return 语句会停止后面的任何代码.(唯一的例外是 return 语句位于 try{} 块中,该块之后有一个 finally{} 块.
This is not true, the return statement will stop any following code. (With the only exception being that the return statement is in a try{} block that has a finally{} block afterwards.
if(0==0){
return;
}
System.out.println("This will not print.");
这篇关于是否“返回"?停止执行方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:是否“返回"?停止执行方法?
基础教程推荐
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01