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
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
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
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
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
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
- Method Control: The return statement provides control over the method's execution flow.
- Flexibility: It allows returning different types of data, including primitives and objects.
- 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.