Java 中的 void 方法中的 return 关键字有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/744676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does the return keyword do in a void method in Java?
提问by Relequestual
I'm looking at a path finding tutorialand I noticed a return
statement inside a void
method (class PathTest
, line 126):
我正在看寻路教程,我注意到方法中的一个return
语句void
(类PathTest
,第 126 行):
if ((x < 0) || (y < 0) || (x >= map.getWidthInTiles()) || (y >= map.getHeightInTiles())) {
return;
}
I'm a novice at Java. Can anyone tell me why it's there? As far as I knew, return
inside a void method isn't allowed.
我是 Java 的新手。谁能告诉我为什么它在那里?据我所知,return
在 void 方法中是不允许的。
采纳答案by CookieOfFortune
It just exits the method at that point. Once return
is executed, the rest of the code won't be executed.
它只是在那时退出该方法。一旦return
被执行,其余的代码将不会被执行。
eg.
例如。
public void test(int n) {
if (n == 1) {
return;
}
else if (n == 2) {
doStuff();
return;
}
doOtherStuff();
}
Note that the compiler is smart enough to tell you some code cannot be reached:
请注意,编译器足够聪明,可以告诉您某些代码无法访问:
if (n == 3) {
return;
youWillGetAnError(); //compiler error here
}
回答by Pesto
You can have return
in a void method, you just can't return any value(as in return 5;
), that's why they call it a voidmethod. Some people always explicitly end void methods with a return statement, but it's not mandatory. It canbe used to leave a function early, though:
您可以return
使用 void 方法,但不能返回任何值(如return 5;
),这就是他们将其称为void方法的原因。有些人总是用 return 语句明确地结束 void 方法,但这不是强制性的。不过,它可用于提前离开函数:
void someFunct(int arg)
{
if (arg == 0)
{
//Leave because this is a bad value
return;
}
//Otherwise, do something
}
回答by John Ellinwood
The Java language specificationsays you can have return with no expression if your method returns void.
Java 语言规范说,如果您的方法返回 void,则您可以不带任何表达式返回。
回答by Chris Ballance
It functions the same as a return for function with a specified parameter, except it returns nothing, as there is nothing to return and control is passed back to the calling method.
它的功能与具有指定参数的函数的 return 相同,只是它不返回任何内容,因为没有任何内容可返回,并且控制权被传递回调用方法。
回答by Albert
It exits the function and returns nothing.
它退出函数并且不返回任何内容。
Something like return 1;
would be incorrect since it returns integer 1.
像return 1;
这样的东西是不正确的,因为它返回整数 1。
回答by MahdeTo
The keyword simply pops a frame from the call stack returning the control to the line following the function call.
该关键字只是从调用堆栈中弹出一个帧,将控制权返回到函数调用之后的行。
回答by iali87
See this example, you want to add to the list conditionally. Without the word "return", all ifs will be executed and add to the ArrayList!
看到这个例子,你想有条件地添加到列表中。没有“return”这个词,所有的ifs都会被执行并添加到ArrayList中!
Arraylist<String> list = new ArrayList<>();
public void addingToTheList() {
if(isSunday()) {
list.add("Pray today")
return;
}
if(isMonday()) {
list.add("Work today"
return;
}
if(isTuesday()) {
list.add("Tr today")
return;
}
}