Reputation:
As I was reading my AP java book I stumbled upon this section that made no sense to me:
...The getBalance method simply returns the current balance. A return statement obtains the value of a variable and exits the method immediately. The return value becomes the value of the method call expression. The syntax of a return statement is:
return expression;
or
return; // Exits the method without sending back a value
Why would you want to have a return statement and "exit the method without sending back a value" if you could just make it void?
***Note: Sorry if this may be too subjective. I just couldn't understand the point and the book doesn't explain it.
Upvotes: 3
Views: 3203
Reputation: 347214
This is kind of subjective. I'm old school, I believe in one entry and exit point for all methods/functions, but...
If you have a method with a void
return type and if you had reached a point in your logic which would dictate that no further processing should take place, you could call return;
to force the execution to return to the calling method at this point, foregoing any further execution of the method.
It would be the same as using something like return x;
in the middle of a method body that had an expected return type (of whatever x
is)
It's a little like using break
to break out of a loop
prematurely, except you're breaking out of the method before the execution gets to the end.
Upvotes: 3
Reputation: 125
In java if a method has a return type in its signature, you must return an object of that type or null before exiting the method.
for example
public A foo(boolean val){
A obj=null;
if (val){
obj=new A();
obj.setSomeAttribute();
}
return obj;
}
You can not compile source code if you just code "return;"
Upvotes: 0
Reputation: 3896
There are some situations where once you've verified something inside a void
function, it makes sense to exit it immediately. For example:
public static void checkIfStringInList(List<String> searchList, String findString) {
for( String thisSearchString : searchList ) {
if( findString.equals(thisSearchString) )
return;
}
throw new Exception("String " + findString + " not found in list");
}
Upvotes: 2
Reputation: 539
The return
keyword doesn't need to be at the end. For example:
public static void print42(int[] numbers) {
for(int i=0; i<numbers.length; i++) {
if (numbers[i] == 42) {
System.out.println("has 42");
return;
}
}
System.out.println("no 42");
}
It can't just use a break
, as that would print both strings.
Upvotes: 3
Reputation: 17598
If your method has a void
return type, then the return
statement is optional. You might want to use it anyway if, for instance, you wanted to stop execution in certain cases.
If the return type is not void, then using return;
without an argument will give you a compile error.
Upvotes: 1
Reputation: 236004
A method declared as void
can exit with a return;
this is just a shortcut for early termination. A method with a non-void
return type must return a value of the right type, you can not have a method return "nothing" if it has a non-void
return type.
Upvotes: 1