Skip to content
Related Articles

Related Articles

Improve Article

return keyword in Java

  • Difficulty Level : Easy
  • Last Updated : 03 Oct, 2017

return is a reserved keyword in Java i.e, we can’t use it as an identifier. It is used to exit from a method, with or without a value.
return can be used with methods in two ways:

  1. Methods returning a value : For methods that define a return type, return statement must be immediately followed by return value.




    // Java program to illustrate usage
    // of return keyword
      
    class A {
      
        // Since return type of RR method is double
        // so this method should return double value
        double RR(double a, double b)
        {
            double sum = 0;
            sum = (a + b) / 2.0;
            // return statement below:
            return sum;
        }
        public static void main(String[] args)
        {
            System.out.println(new A().RR(5.5, 6.5));
        }
    }

    Output:

    6.0
    

    Flow of program: When we call A class’s RR method that has return sum which returns the value of sum and that’s value gets displayed on the console.

  2. Methods not returning a value : For methods that don’t return a value, return statement can be skipped.
    • Example 1: Method NOT using return statement in void function:




      // Java program to illustrate no return
      // keyword needed inside void method
        
      class GFG {
        
          // Since return type of RR method is
          // void so this method shouldn't return any value
          void RR(int a, int b)
          {
              int sum = 0;
              sum = (a + b) / 10;
              System.out.println(sum);
              // no return statement in this method
          }
        
          public static void main(String[] args)
          {
              new GFG().RR(5, 5);
          }
      }

      Output:

      1
      
    • Example 2 : Methods with return type void : return statement not required (but can be used) for methods with return type void. We can use “return;” which means not return anything.




      // Java program to illustrate usage of
      // return keyword in void method
        
      class K {
          // Since return type of RR method is
          // void so this method should not return any value
          void RR(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;
          }
          public static void main(String[] args)
          {
              new K().RR(5.5);
              System.out.println("In main");
          }
      }

      Output:

      In main
      

      Flow of the program: If the statement if(j<9) is true then control exits from the RR method and does not execute the rest of the statement of the RR method and hence come back again to main method.

Using return statement at the end of 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.




// Java program to illustrate that return
// need not be last statement,
// but must be last statement in a method to execute
  
class H {
  
    // Since return type of RR method is void
    // so this method should not return any value
    void RR(double i)
    {
        if (i < 9)
  
            // see here return need not be last
            // statement but must be last statement
            // in a method to execute
            return;
  
        else
            ++i;
    }
    public static void main(String[] args)
    {
        new H().RR(7);
        System.out.println("In main");
    }
}

Output:

In main

Flow: As the condition (i<9) becomes true then it executes return statement and hence flow comes out of RR method and comes back again to main.
Following this, return statement must be the last statement to execute in a method, which means there is no point in defining any code after return. For example compiler fail to compile this:




// Java program to illustrate usage of
// statement after return statement
  
class Z {
  
    // Since return type of RR method is void
    // so this method should return any value
    void RR(double j)
    {
        return;
  
        // here get compile error since can't
        // write any statement after return keyword
        ++j;
    }
      
    public static void main(String[] args)
    {
        new Z().RR(5);
    }
}

Output:

Compilation error: unreachable statement ++j;

One more Example




// Java program to illustrate usage
// of return keyword
class GFG {
  
    // Since return type of RR method is
    // void so this method should not return any value
    void oshea(double val)
    {
        if (val < 0) {
            System.out.println(val);
            return;
  
            // if we uncomment below then, get compilation
            // error because after return can't write any 
            // statement
            // System.out.println("oshea");
  
        } else
            ++val;
    }
    public static void main(String[] args)
    {
        new GFG().oshea(-1);
        System.out.println("In main");
    }
}

Output:

-1.0
In main

This article is contributed by Rajat Rawat. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :