Mathod Return

What is a return statement in Java?

In Java programming, the return statement is used to return a value when the execution of the block is completed. The return statement inside a loop will cause the loop to break and other statements will be ignored by the compiler.

Returning a Value from a Method

Every method in Java is declared with a return type and it is mandatory for all Java Method. A return type may be a primitive type like int, float, double , String and void type(returns nothing).

A return statement had to be included at the end of the method for these return types. The return keyword is used to return the resulting value.

There is no need for a return statement when using the void return type. When attempting to retrieve a value from a void method, an error is displayed by the compiler.

These are the important points to remember while returning a value:

  1. The return type of the method and the type of data returned at the end of the method should be the same. For example, if a method is declared as float return, the returned value must be float only.
  2. A similar data type should be used for the variable that stores the returned value after the method is called, otherwise the data could get lost.
  3. If a method is declared with parameters, the parameter sequence should be the same as any method statement and call.

The syntax of a return statement is the return keyword is followed by the value to be returned.

return returnvalue;

By following Java programs, we demonstrate the use of return statements.

A method with a non-void return type would look like this:

public class MethodWithReturn {
    public static void main(String[] args) {
        int sumValue = sumUpTwoNumber(2,7); //return type method calling
                System.out.println("Sum=" + sumValue);
    }//main
    
    public static int sumUpTwoNumber(int a, int b) {
        int r;
        r = a + b;
        return r;
    }//sumUpTwoNumber
    
}//class

Explanation:

  1. public class MethodWithReturn : This line defines a public class named MethodWithReturn .
  2. public static void main(String[] args) : This line defines the main method, which serves as the entry point of the program. It takes a String array args as its parameter, allowing you to pass command-line arguments to the program.
  3. int sumValue = sumUpTwoNumber(2,7); : In this line, a variable sumValue of type int is declared and initialized. It calls the sumUpTwoNumber method with the arguments 2 and 7, and the returned value is stored in the sumValue variable. The sumUpTwoNumber method calculates the sum of the two integers and returns the result, which is assigned to sumValue.
  4. System.out.println(“Sum=” + sumValue); : System.out.println() method is used to display the output. It takes a string as its argument, which is composed of the text “Sum=” concatenated with the value of the sumValue variable. The + operator is used for string concatenation, converting the integer sumValue to a string before combining it with the rest of the text. The output will display “Sum=9” in this case, as the sumValue variable holds the value 9.
  5. public static int sumUpTwoNumber(int a, int b) : This line defines a new method called sumUpTwoNumber, which is declared as public, static, and has a return type of int. The method takes two parameters of type int named a and b.
  6. int r; : Here, a local variable r of type int is declared. It will be used to store the result of the addition of a and b.
  7. r = a + b; : In this line, the variables a and b are added together, and the result is stored in the r variable.
  8. return r; : The return statement is used to send the value of the r variable back to the caller of the sumUpTwoNumber method. As the return type of the method is int, the value of r (which holds the sum of a and b) is returned.

To summarize, this Java program defines a class with a method that calculates the sum of two numbers and returns the result. The main method calls this method and displays the sum on the console. The program demonstrates how to use methods with return types to encapsulate reusable logic and retrieve results from method calls.

Output:

Sum=9

Another example of a method with a non-void return type:

public class RectangleCalculator {
   
    public static void main(String[] args) {
        int rectangleWidth = 5;
        int rectangleHeight = 8;

        int areaOfRectangle = calculateRectangleArea(rectangleWidth, rectangleHeight);
        System.out.println("Area of the rectangle: " + areaOfRectangle); // Output: 40
    }

    public static int calculateRectangleArea(int width, int height) { 
        int area = width * height; 
        return area; 
    }
}

Explanation:

  1. public class RectangleCalculator : This line defines a public class named RectangleCalculator .
  2. public static void main(String[] args) : This line defines the main method, which serves as the entry point of the program. It takes a String array args as its parameter, allowing you to pass command-line arguments to the program.
  3. int rectangleWidth = 5;: In this line, a new integer variable named rectangleWidth is declared and initialized with the value 5. This variable represents the width of the rectangle.
  4. int rectangleHeight = 8;: In this line, another integer variable named rectangleHeight is declared and initialized with the value 8. This variable represents the height of the rectangle.
  5. int areaOfRectangle = calculateRectangleArea(rectangleWidth, rectangleHeight);: In this line, a new integer variable named areaOfRectangle is declared. The calculateRectangleArea method is called with rectangleWidth and rectangleHeight as arguments. The method calculates the area of the rectangle using the provided width and height and returns the result. The returned value is stored in the areaOfRectangle variable.
  6. System.out.println(“Area of the rectangle: ” + areaOfRectangle);: In this line, the System.out.println() method is used to display the output on the console. It takes a string as its argument, which is created by concatenating the text “Area of the rectangle: ” with the value of the areaOfRectangle variable. The + operator is used for string concatenation, converting the integer areaOfRectangle to a string before combining it with the rest of the text. Given that the areaOfRectangle variable contains the estimated area of the rectangle, which is 40, the output will read “Area of the rectangle: 40”.
  7. public static int calculateRectangleArea(int width, int height) : This line defines a method named calculateRectangleArea with a return type of int. The method is declared as public and static, which means it can be accessed from other classes without creating an instance of the class. It takes two integer parameters: width and height, which represent the dimensions of the rectangle.
  8. int area = width * height;: In this line, a new local variable area of type int is declared. The variable area will be used to store the calculated area of the rectangle. The area is calculated by multiplying the width and height parameters.
  9. return area;: The return statement is used to send the value of the area variable back to the caller of the calculateRectangleArea method. Since the return type of the method is int, it must return an integer value. In this case, the calculated area is returned.

In summary, this Java program defines a class RectangleCalculator with a method calculateRectangleArea that calculates the area of a rectangle based on its width and height. The main method demonstrates how to use the calculateRectangleArea method to calculate and print the area of a rectangle with a width of 5 and a height of 8. The output will be “Area of the rectangle: 40“, as the calculated area is 5 * 8 = 40.

Output:

Area of the rectangle: 40

 

Visited 1 times, 1 visit(s) today

Comments are closed.

Close