return keyword in Java
In Java, return is a reserved keyword i.e, we can’t use it as an identifier. It is used to exit from a method, with or without a value. Usage of return keyword as there exist two ways as listed below as follows:
- Case 1: Methods returning a value
- Case 2: Methods not returning a value
Let us illustrate by directly implementing them as follows:
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Case 1: Methods returning a value
For methods that define a return type, return statement must be immediately followed by return value.
Output explanation: When we are calling a class GFG method that has return sum which returns the value of sum and that’s value gets displayed on the console.
Case 2: Methods not returning a value
For methods that do not return a value, return statement in Java can be skipped. here there arise two cases when there is no value been returned by the user as listed below as follows:
- #1: Method not using return statement in void function
- #2: Methods with return type void
#1: Method not using return statement in void function
1 No return keyword is used and program executed successfully
Note: Return statement not required (but can be used) for methods with return type void. We can use “return;” which means not return anything.
#2: Methods with void return type
Program executed successfully
Output explanation: If the statement if(j<9) is true then control exits from the method and does not execute the rest of the statement of the RR method and hence comes back again to main() method.
Now moving ahead geek you must be wondering what if we do use return statement at the end of the program?
return statement can be used at various places in the method but we need to ensure that it must be the last statement to get executed in a method.
Note: return statement need not to be last statement in a method, but it must be last statement to execute in a method.
Program executed successfully
As the condition (i<9) becomes true, it executes return statement, and hence flow comes out of ‘demofunction’ method and comes back again to main. Following this, the return statement must be the last statement to execute in a method, which means there is no point in defining any code after return which is clarified below as follows:
-1.0 Program Executed Successfully
Note: In the above program we do uncomment statements it will throw an error.
This article is contributed by Rajat Rawat. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.