java - if条件中的return语句函数

标签 java if-statement return

在我的代码中:

if (id.isEmpty() || name.isEmpty()) {
    warlbl.setText("Warning, Empty ID or Name Fields");
    return;
}

idname 是从 JTextFields 给出的 String

这里是否需要使用return;

最佳答案

是的,它可以是:

if (...) {
    ...
    return;
}

// nothing at this point will be reached if the if-statement is entered

对比

if (...) {
    ...
}

// code here will still be reached!

关于java - if条件中的return语句函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18038127/

相关文章:

java - 为什么这个通配符有效?

java - Esper 合并查询结果

java - 编码(并重定向)到在 Java 中具有特殊字符的 URL

vba - 嵌套循环和If,不执行循环

ios - Xcode 10.1 "return"语句不会停止函数执行

java - 如何将 Retrofit 和 RxJava 与多个 pojo 类一起使用?

python - 如何避免 python 字典中的重复 if 语句

javascript - 使用 if 语句更改多个变量

java - 为什么我不能在另一个类中访问我的静态变量

java - 从方法返回一个字符