Open In App
Related Articles

return keyword in Java

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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

// Java program to illustrate usage of
// return keyword in void method
 
// Class 1
// Main class
class GFG {
 
    // Method 1
    // Since return type of RR method is
    // void so this method should not return any value
    void demofunction(double j)
    {
        if (j < 9)
 
            // return statement below(only using
            // return statement and not returning
            // anything):
            // control exits the method if this
            // condition(i.e, j<9) is true.
            return;
        ++j;
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
        GFG gfg = new GFG();
        // Calling above method declared in above class
        gfg.demofunction(5.5);
 
        // Display message on console to illustrate
        // successful execution of program
        System.out.println("Program executed successfully");
    }
}

                    

Output
6.0

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

<div id="highlighter_802277" class="syntaxhighlighter nogutter  "><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="comments">// Java program to illustrate no return</code></div><div class="line number2 index1 alt1"><code class="comments">// keyword needed inside void method</code></div><div class="line number3 index2 alt2"> </div><div class="line number4 index3 alt1"><code class="comments">// Main class</code></div><div class="line number5 index4 alt2"><code class="keyword">class</code> <code class="plain">GFG {</code></div><div class="line number6 index5 alt1"> </div><div class="line number7 index6 alt2"><code class="undefined spaces">    </code><code class="comments">// Since return type of RR method is</code></div><div class="line number8 index7 alt1"><code class="undefined spaces">    </code><code class="comments">// void so this method shouldn't return any value</code></div><div class="line number9 index8 alt2"><code class="undefined spaces">    </code><code class="keyword">void</code> <code class="plain">demoSum(</code><code class="keyword">int</code> <code class="plain">a, </code><code class="keyword">int</code> <code class="plain">b)</code></div><div class="line number10 index9 alt1"><code class="undefined spaces">    </code><code class="plain">{</code></div><div class="line number11 index10 alt2"><code class="undefined spaces">        </code><code class="keyword">int</code> <code class="plain">sum = </code><code class="value">0</code><code class="plain">;</code></div><div class="line number12 index11 alt1"><code class="undefined spaces">        </code><code class="plain">sum = (a + b) / </code><code class="value">10</code><code class="plain">;</code></div><div class="line number13 index12 alt2"><code class="undefined spaces">        </code><code class="plain">System.out.println(sum);</code></div><div class="line number14 index13 alt1"> </div><div class="line number15 index14 alt2"><code class="undefined spaces">        </code><code class="comments">// No return statement in this method</code></div><div class="line number16 index15 alt1"><code class="undefined spaces">    </code><code class="plain">}</code></div><div class="line number17 index16 alt2"> </div><div class="line number18 index17 alt1"><code class="undefined spaces">    </code><code class="comments">// Method 2</code></div><div class="line number19 index18 alt2"><code class="undefined spaces">    </code><code class="comments">// Main driver method</code></div><div class="line number20 index19 alt1"><code class="undefined spaces">    </code><code class="keyword">public</code> <code class="keyword">static</code> <code class="keyword">void</code> <code class="plain">main(String[] args)</code></div><div class="line number21 index20 alt2"><code class="undefined spaces">    </code><code class="plain">{</code></div><div class="line number22 index21 alt1"><code class="undefined spaces">        </code><code class="comments">// Calling the method</code></div><div class="line number23 index22 alt2"><code class="undefined spaces">        </code><code class="comments">// Over custom inputs</code></div><div class="line number24 index23 alt1"><code class="undefined spaces">        </code><code class="keyword">new</code> <code class="plain">GFG().demoSum(</code><code class="value">5</code><code class="plain">, </code><code class="value">5</code><code class="plain">);</code></div><div class="line number25 index24 alt2"> </div><div class="line number26 index25 alt1"><code class="undefined spaces">        </code><code class="comments">// Display message on the console for successful</code></div><div class="line number27 index26 alt2"><code class="undefined spaces">        </code><code class="comments">// execution of the program</code></div><div class="line number28 index27 alt1"><code class="undefined spaces">        </code><code class="plain">System.out.print(</code></div><div class="line number29 index28 alt2"><code class="undefined spaces">            </code><code class="string">"No return keyword is used and program executed successfully"</code><code class="plain">);</code></div><div class="line number30 index29 alt1"><code class="undefined spaces">    </code><code class="plain">}</code></div><div class="line number31 index30 alt2"> </div><div class="line number32 index31 alt1"><code class="undefined spaces">    </code><code class="comments">// Note here we are not returning anything</code></div><div class="line number33 index32 alt2"><code class="undefined spaces">    </code><code class="comments">// as the return type is void</code></div><div class="line number34 index33 alt1"><code class="plain">}</code></div></div></td></tr></tbody></table></div>

                    

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

// Java program to illustrate usage of
// statement after return statement
 
// Main class
class GFG {
 
    // Since return type of RR method is void
    // so this method should return any value
    // Method 1
    void demofunction(double j)
    {
        return;
 
        // Here get compile error since can't
        // write any statement after return keyword
 
        ++j;
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
 
        // Calling the above defined function
        new GFG().demofunction(5);
    }
}

                    

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

// Java program to illustrate usage
// of return keyword
 
// Main class
class GFG {
 
    // Since return type of RR method is
    // void so this method should not return any value
    // Method 1
    void demofunction(double val)
    {
 
        // Condition check
        if (val < 0) {
 
            System.out.println(val);
            return;
 
            // System.out.println("oshea");
        }
        else
            ++val;
    }
 
    // Method 2
    // Main drive method
    public static void main(String[] args)
    {
 
        // CAlling the above method
        new GFG().demofunction(-1);
 
        // Display message to illustrate
        // successful execution of program
        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


                    

Output:

Example 2-B

Java

// Java program to illustrate usage
// of return keyword
 
// Main class
class GFG {
 
    // Since return type of RR method is
    // void so this method should not return any value
    // Method 1
    void demofunction(double val)
    {
 
        // Condition check
        if (val < 0) {
 
            System.out.println(val);
            return;
 
            // System.out.println("oshea");
        }
        else
            ++val;
    }
 
    // Method 2
    // Main drive method
    public static void main(String[] args)
    {
 
        // CAlling the above method
        new GFG().demofunction(-1);
 
        // Display message to illustrate
        // successful execution of program
        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.

 



Last Updated : 17 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads