Table of contents
1.
Introduction
2.
What is a Return Statement in Java?
2.1.
Java
3.
Use Cases of Return Keyword in Java
3.1.
Java
3.2.
Java
3.3.
Java
4.
How to Return a Value from a Method in Java?
4.1.
Java
5.
Returning an Object (Class) or Interface in Java
5.1.
Java
6.
Key Features of Return Type In Java
7.
Frequently Asked Questions
7.1.
Can we have multiple return statements in a single method?
7.2.
What happens if a method with a non-void return type does not have a return statement?
7.3.
What is the purpose of the return statement in Java?
7.4.
What is the difference between print and return in Java?
8.
Conclusion
Last Updated: Dec 12, 2024
Easy

Return Statement in Java

Author Rahul Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Java, the return statement is used to exit a method and optionally return a value to the caller. It is an essential part of methods, helping in providing the method's result back to where it was invoked. 

Return Statement in Java

Understanding the return statement is crucial for writing effective Java programs.

What is a Return Statement in Java?

The return statement can be used in any method to stop its execution and return control to the calling method. It can be used with or without a value, depending on whether the method is declared to return a value.

Syntax:

return;
or
java
return value;

Example:

  • Java

Java

public class ReturnExample {

   public static void main(String[] args) {

       int result = addNumbers(5, 10);

       System.out.println("Sum: " + result);

   }


   public static int addNumbers(int a, int b) {

       return a + b;  // returns the sum of a and b

   }

}
You can also try this code with Online Java Compiler
Run Code


Output

Sum: 10


In this example, the addNumbers method calculates the sum of two numbers and returns the result using the return statement.

Use Cases of Return Keyword in Java

The return keyword has several use cases:

1. Methods which return a value

The return keyword is used to send a value back to the calling method. It terminates the method execution and provides the specified value.

Example:
 

  • Java

Java

public class Main {
public static int add(int a, int b) {
return a + b;
}

public static void main(String[] args) {
int result = add(5, 10);
System.out.println("Sum: " + result);
}
}
You can also try this code with Online Java Compiler
Run Code

Output:

Sum: 15

 

2. Methods which do not return a value

Here two cases arise when no value been returned by the user:-

  • Method not using return statement in void function

When a void method doesn't use return, it completes execution without returning anything explicitly.

Example:

  • Java

Java

public class Main {
public static void printMessage() {
System.out.println("Hello, World!");
}

public static void main(String[] args) {
printMessage();
}
}
You can also try this code with Online Java Compiler
Run Code

Output:

Hello, World!

 

  • Methods with return type void 

A void method can use return to terminate early without providing any value.

Example:

  • Java

Java

public class Main {
public static void checkNumber(int number) {
if (number < 0) {
System.out.println("Negative number.");
return;
}
System.out.println("Non-negative number.");
}

public static void main(String[] args) {
checkNumber(-5);
checkNumber(10);
}
}
You can also try this code with Online Java Compiler
Run Code

Output:

Negative number.
Non-negative number.

How to Return a Value from a Method in Java?

To return a value from a method in Java, you define the method with a return type that matches the value you want to return. Use the return keyword to send the value back to the calling method. The method terminates when the return statement is executed. For example:

public int square(int number) {
    return number * number;
}

 

Here, the method square returns an integer value. In the calling method, you can store or use the returned value:

int result = square(5);
System.out.println(result); // Output: 25

 

Syntax:

public returnType methodName() {
    // method body
    return value;
}

Example:

  • Java

Java

public class ReturnValueExample {

   public static void main(String[] args) {

       int square = getSquare(4);

       System.out.println("Square: " + square);

   }

   public static int getSquare(int number) {

       return number * number;

   }

}
You can also try this code with Online Java Compiler
Run Code

 

Output

Square: 16


In this example, getSquare method takes an integer and returns its square.

Returning an Object (Class) or Interface in Java

In Java, methods can return objects. This is particularly useful for object-oriented programming, where methods can return instances of classes.

Syntax:

public ClassName methodName() {
    // method body
    return new ClassName();
}

Example:

  • Java

Java

class Student {

   String name;

   int age;

   Student(String name, int age) {

       this.name = name;

       this.age = age;

   }

}

public class ReturnObjectExample {

   public static void main(String[] args) {

       Student student = getStudent();

       System.out.println("Name: " + student.name + ", Age: " + student.age);

   }

   public static Student getStudent() {

       return new Student("John", 20);

   }

}
You can also try this code with Online Java Compiler
Run Code


Output

Name: John, Age: 20


In this example, the getStudent method returns a Student object.

Key Features of Return Type In Java

  1. Method Control: The return statement provides control over the method's execution flow.
     
  2. Flexibility: It allows returning different types of data, including primitives and objects.
     
  3. Code Clarity: Enhances code readability by clearly showing the exit points of a method.

Frequently Asked Questions

Can we have multiple return statements in a single method?

Yes, you can have multiple return statements in a single method, but only one will be executed based on the program flow.

What happens if a method with a non-void return type does not have a return statement?

If a method with a non-void return type does not have a return statement, the compiler will throw an error.

What is the purpose of the return statement in Java?

The return statement in Java is used to exit a method and optionally pass a value back to the calling method. It ensures control is returned to the caller with or without a computed result.

What is the difference between print and return in Java?

The print statement displays information on the console, primarily for user output or debugging. In contrast, the return statement exits a method and optionally passes a value to the calling method, facilitating data flow between methods.

Conclusion

The Java return statement is a powerful feature that allows methods to return values and control the flow of execution. It can be used to return primitive data types, objects, and even exit methods early. Understanding how to use the return statement effectively is essential for writing clear and efficient Java code.

Live masterclass