return keyword in Java is a reserved keyword which is used to exit from a method, with or without a value. The usage of the return keyword can be categorized into two cases:
- Methods returning a value
- Methods not returning a value
1. Methods Returning a Value
For the methods that define a return type, the return statement must be immediately followed by a return value.
Example:
Java
// Java Program to Illustrate Usage of return Keyword
class Geeks {
// Method to calculate average
double avg(double x, double y) {
double res = (x + y) / 2.0;
return res; // Return the calculated result
}
// Main driver method
public static void main(String[] args) {
System.out.println(new Geeks().avg(5.5, 6.5));
}
}
Explanation: In the above example, when the method avg is called, it computes the average of the inputs and returns the result, which is printed to the console.
2. Methods Not Returning a Value
For methods that do not return a value, the return statement can be skipped. This case can be further divided into two scenarios:
- Method Without return in a void Function
- Methods With void Return Type
Example 1: Method Without return in a void Function
Java
// Java program to illustrate no return
// keyword needed inside void method
class Geeks {
// Method to calculate a reduced sum
void calc(int a, int b) {
int res = (a + b) / 10;
System.out.println(res);
}
// Main driver method
public static void main(String[] args) {
new Geeks().calc(5, 5);
System.out.println("No return keyword is used, and program executed successfully");
}
}
Output1
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.
Example 2: Methods With void Return Type
Java
// Java program to illustrate usage
// of return keyword in void method
class Geeks {
// Method to check a condition
void check(double v) {
if (v < 9) {
return; // Exit method if condition is met
}
v++;
}
// Main driver method
public static void main(String[] args) {
Geeks o = new Geeks();
o.check(5.5);
System.out.println("Program executed successfully");
}
}
OutputProgram executed successfully
Explanation: In the above example, if the condition (v < 9) is true, the control exits the method without executing the remaining statements.
Using return Statement at the End of the Program
The 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.
1. Return Statement in a Void Method
Java
// Java program to illustrate that return is not always
// the last statement but must be the last to execute
class Geeks {
// Helper method with void return type
void demoFunc(double n) {
// Exit method if condition is met
if (n < 9) return;
// Increment the number otherwise
n++;
}
// Main method
public static void main(String[] args) {
// Call the helper method
new Geeks().demoFunc(7);
System.out.println("Program executed successfully");
}
}
OutputProgram executed successfully
Explanation: In the above example, the return statement in Java does not need to be the last line in a method but must be the last to execute. The demoFunc method exits early when the condition is met, and the program confirms successful execution.
2. Unreachable Return Statement Causes Compile-Time Error
Java
// Java program to illustrate usage of
// statement after the return statement
public class Geeks {
// Helper method with void return type
void demoFunc(double j) {
// Exit the method
return;
// The following line will result in a compile-time
// error as it is unreachable
++j;
}
public static void main(String[] args) {
// Call the method
new Geeks().demoFunc(5);
}
}
Output: This code will not compile due to the unreachable statement after return.
3. Using Return Statement Inside a Condition
Java
// Java program to illustrate
// usage of return keyword
class Geeks {
// Method with void return type
void demo(double v) {
// Condition check
if (v < 0) {
System.out.println(v);
return;
} else {
++v;
}
}
public static void main(String[] args) {
// Call the method
new Geeks().demo(-1);
// Display message to illustrate successful execution
System.out.println("Program executed successfully");
}
}
Output-1.0
Program executed successfully
Explanation: In the above example, the return statement allows the method to exit early when the condition is met, avoiding the execution of the increment operation.
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java