1. Overview
1.概述
In this tutorial, we’re looking at a common mistake in the Java development process. Usually, beginners face this problem, the missing return statement error in the Java application.
在本教程中,我们要研究的是Java开发过程中的一个常见错误。通常情况下,初学者都会面临这样的问题,即Java应用程序中缺少返回语句的错误。
The missing return statement error is a compile-time error. It’s thrown during the compilation phase. Modern IDEs lint this error on the fly. Therefore this type of error tends to be easy to detect.
缺少返回语句的错误是一个编译时错误。它是在编译阶段抛出的。现代集成开发环境在运行中会对这个错误进行提示。因此,这种类型的错误往往很容易被发现。
The main causes are:
主要原因是。
- a return statement was simply omitted by mistake
- the method doesn’t return any value, but type void is not declared in the method signature
2. Missing Return Statement
2.遗失退货声明
First, we’re going to see a couple of examples. These examples are related to the return statement omitted by mistake. Then, we’ll be looking for an example of the missing void type in the method signature. Each example shows how we can resolve the java missing return statement error.
首先,我们要看几个例子。这些例子与错误省略的返回语句有关。然后,我们要找一个方法签名中缺少void类型的例子。每个例子都显示了我们如何解决java丢失返回语句的错误。
2.1. Omitted Return Statement
2.1.遗漏的返回声明
Next, let’s define a simple pow method:
接下来,让我们定义一个简单的pow方法。
public int pow(int number) {
int pow = number * number;
}
As a result of compiling the previous code, we get:
作为编译先前代码的结果,我们得到了。
java: missing return statement
In order to fix this problem, we just add a return statement after the pow variable:
为了解决这个问题,我们只需在pow变量后添加一个返回语句。
public int pow(int number) {
int pow = number * number;
return pow;
}
Consequently, if we call method pow, we get the expected result.
因此,如果我们调用方法pow,我们会得到预期的结果。
Similarly, but with conditional structures, this error arises:
类似地,但对于条件性结构,会出现这种错误。
public static String checkNumber(int number) {
if (number == 0) {
return "It's equals to zero";
}
for (int i = 0; i < number; i++) {
if (i > 100) {
return "It's a big number";
}
}
}
The above code checks an input number. First, compare the input number with 0. If the condition is true, a string value is returned. Then, if the number is greater than 0, we find a for-loop with an inner condition. Our conditional statement inside for-loop is satisfied if “i” is greater than 100. But, what about a negative input number?. Yes, you are right. We miss a default return statement. Therefore, if we compile our code, we get the java: missing return statement error again.
上面的代码检查一个输入的数字。首先,将输入的数字与0进行比较,如果条件为真,就会返回一个字符串值。然后,如果数字大于0,我们找到一个带有内部条件的for-loop。如果”i“大于100,我们在for-loop中的条件语句就得到满足。但是,如果输入的数字是负数呢?是的,你是对的。我们错过了一个默认的返回语句。因此,如果我们编译我们的代码,我们又会得到java: missing return statement错误。
So, in order to fix it, we just need to put a default return statement at the end of the method:
所以,为了解决这个问题,我们只需要在方法的末尾放一个默认的返回语句。
public static String checkNumber(int number) {
if (number == 0) {
return "It's equals to zero";
}
for (int i = 0; i < number; i++) {
if (i > 100) {
return "It's a big number";
}
}
return "It's a negative number";
}
2.2. Missing Return in Lambdas
2.2.在Lambdas中缺失的返回
Besides, when we work with lambdas, this error could appear. For functions, it could be a little tricky to detect this error. The map method in streams is a common place where this error happens. Let’s inspect our code:
此外,当我们使用lambdas时,这个错误可能会出现。对于函数来说,检测这个错误可能有点棘手。map流中的方法是发生这个错误的常见地方。让我们检查一下我们的代码。
public Map<String, Integer> createDictionary() {
List<String> words = Arrays.asList("Hello", "World");
Map<String, Integer> dictionary = new HashMap<>();
words.stream().map(s -> {dictionary.put(s, 1);});
return dictionary;
}
The previous code looks fine. There is a return statement. Our return data type is equal to the method signature. But, what about the code inside the map method in the stream?. The map method expects a function as the argument. In this case, we only put data into our dictionary inside the map method. As a result, If we try to compile this code, we get the java: missing return statement error again.
前面的代码看起来很好。有一个返回语句。我们的返回数据类型等同于方法签名。但是,流中的map方法里面的代码呢?map方法期望一个函数作为参数。在这种情况下,我们只在map方法内将数据放入我们的字典。结果,如果我们试图编译这段代码,我们又会得到java: missing return statement错误。
Next, to resolve the error, we simply replace the map with forEach method in the stream:
接下来,为了解决这个错误,我们只需将map替换为forEach流中的方法。
words.forEach(s -> {dictionary.put(s, 1);});
Or, directly return a map from the stream:
或者,直接从流中返回一个地图。
dictionary = words.stream().collect(Collectors.toMap(s -> s, s -> 1))
2.3. Missing Method Signature
2.3.缺少方法签名
Finally, the last case is when we missed adding a return type to our method signature. Consequently, when we try to compile our method, we get an error. The following code example shows us this behavior:
最后一种情况是,我们错过了在方法签名中添加一个返回类型。因此,当我们试图编译我们的方法时,我们得到一个错误。下面的代码例子向我们展示了这种行为。
public pow(int number) {
int pow = number * number;
return pow;
}
We forgot to add the int as return type. If we add it to our method signature will fix this error:
我们忘记了将int作为返回类型。如果我们把它添加到我们的方法签名中,将解决这个错误。
public int pow(int number) {
int pow = number * number;
return pow;
}
3. Conclusion
3.总结
In this article, we went through some examples of missing return statements. How it can appear in our code, and how we can fix it. This is useful to avoid future mistakes in our code and maybe a couple of minutes of code inspection.
在这篇文章中,我们经历了一些丢失返回语句的例子。它如何出现在我们的代码中,以及我们如何修复它。这对避免我们的代码将来出现错误是很有帮助的,也许还需要几分钟的代码检查。
As usual, all snippets used in this article are available over on GitHub.
像往常一样,本文中使用的所有片段都可以在GitHub上找到。