What does return mean at the end of a void method?
I know that a void
method does not return any value, but I can still write code like
void nothingDohere() {return;}
So, How can "void" work with the "retur开发者_开发知识库n" statement here?
A return statement without a value can only be used in a void
method (or a constructor), and simply performs the "get out of the method now" part of returning. Think of a return-with-value statement has having two purposes:
- Terminating execution of the current method (via
finally
blocks of course) - Making a value computed in the method available to the caller
The return statement in a void
method simply achieves the first of these; the second makes no sense in a void
method.
From section 14.17 of the Java Language Specification:
A return statement with no Expression must be contained in the body of a method that is declared, using the keyword void, not to return any value (§8.4), or in the body of a constructor (§8.8). A compile-time error occurs if a return statement appears within an instance initializer or a static initializer (§8.7). A return statement with no Expression attempts to transfer control to the invoker of the method or constructor that contains it. To be precise, a return statement with no Expression always completes abruptly, the reason being a return with no value.
Your return statement has no argument, so it is not returning anything, hence the returned value is 'void'.
At any point in a Method when return statement is executed it simple exits out of the method.So return here is just ending the execution of you method here.
If you do try to something (some value) from a void method e.g
return"xzy";
You will get complition error Void methods cannot return a value
The return
statement can be also used to cause execution to go back to the caller of this method. Thus, the return statement will immediately terminate the method in which it is executed.
Therefore, your sample code:
void nothingDohere() {
return ;
}
Terminates the method call and returns back immediately to the caller of the method.
In the case mentioned by you the return is indeed semantically superfluous (it is though needed syntactically - Javac won't compile without this return). The return terminates the method.
However in some cases it is wished to have multiple exit poins in a method, e.g. like this:
public void doDifferentThings(){
if(){
//something done
return;
}
else{
//something else done
return;
}
}
Some consider this bad design (contradicts single-point-of-exit), however it can keep your code cleaner.
Simply, it returns from inside the void method to the position where the program called that method:
public calss Test
{
private boolean flag = true;
public Test()
{
... // (1)
... // (2)
voidMethod(); // (3)
... // (7)
... // (8)
}
private void voidMethod()
{
... // (4)
... // (5)
if(flag) return; // (6)
...
...
}
}
When you return from a function, you return the control to the function calling the returning function. If you return a value, the calling function will get that value, else the calling function will only get the control.
精彩评论