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:
Case 1: Methods returning a value
For methods that define a return type, return statement must be immediately followed by return value.
Example:
Java
class GFG {
double RR( double a, double b) {
double sum = 0 ;
sum = (a + b) / 2.0 ;
return sum;
}
public static void main(String[] args)
{
System.out.println( new GFG().RR( 5.5 , 6.5 ));
}
}
|
Time Complexity: O(1)
Auxiliary Space : O(1)
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
Example
Java
class GFG {
void demoSum( int a, int b)
{
int sum = 0 ;
sum = (a + b) / 10 ;
System.out.println(sum);
}
public static void main(String[] args)
{
new GFG().demoSum( 5 , 5 );
System.out.print(
"No return keyword is used and program executed successfully" );
}
}
|
Output
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
Example 1-A:
Java
class GFG {
void demofunction( double j)
{
if (j < 9 )
return ;
++j;
}
public static void main(String[] args)
{
GFG gfg = new GFG();
gfg.demofunction( 5.5 );
System.out.println( "Program executed successfully" );
}
}
|
Output
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.
Example 1-B:
Java
class GFG {
void demofunction( double i)
{
if (i < 9 )
return ;
else
++i;
}
public static void main(String[] args)
{
new GFG().demofunction( 7 );
System.out.println( "Program executed successfully" );
}
}
|
Output
Program executed successfully
Output explanation:
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:
Example 2A
Java
class GFG {
void demofunction( double j)
{
return ;
++j;
}
public static void main(String[] args)
{
new GFG().demofunction( 5 );
}
}
|
Output:

Example 2-B
Java
class GFG {
void demofunction( double val)
{
if (val < 0 ) {
System.out.println(val);
return ;
}
else
++val;
}
public static void main(String[] args)
{
new GFG().demofunction(- 1 );
System.out.println( "Program Executed Successfully" );
}
}
|
Output
-1.0
Program Executed Successfully
Note: In the above program we do uncomment statements it will throw an error.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
17 Aug, 2022
Like Article
Save Article