Which of these data types can be used for a method having a return statement in it?

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: Whenwe 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:

Which of these data types can be used for a method having a return statement in it?

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.

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 . 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.


Which of these data type can be used for a method having a return statement in it * a void B int c float d both int and float?

Which of these data type can be used for a method having a return statement in it? Explanation: None.

What is the use of return statement in Java?

A return statement causes the program control to transfer back to the caller of a method. Every method in Java is declared with a return type and it is mandatory for all java methods. A return type may be a primitive type like int, float, double, a reference type or void type(returns nothing).

What is the default return type of a method in Java language?

The default returns value from a function in int. In other words generally unless explicitly specified the default return value by compiler would be integer value from function.

Which of these can be used to differentiate two or more methods having the same name?

Explanation: Two or more methods can have same name as long as their parameters declaration is different, the methods are said to be overloaded and process is called method overloading.