⏱️ 7 min read • Beginner Level • Lesson 32
Completed on . You can revise this lesson or continue to the next topic.
The return statement in Java is used to return control from a method back to the place where the method was called. It can also return a value from a method.
Before learning return, you should understand
Java methods,
Java data types,
Java if statement, and
Java continue statement.
The return statement is used inside a method. When Java executes
return, the method stops immediately and control goes back to the caller.
return can stop a method immediately.
It can return a value from a method.
The returned value must match the method return type.
There are two common forms of return statement:
return; // used in void methods
return value; // used in methods that return a value
void, it cannot return a value.
If a method return type is int, String, boolean,
or any other type, it must return a compatible value.
In a void method, return; is used to stop the method early.
It does not return any value.
public class ReturnInVoidMethod {
static void checkAge(int age) {
if (age < 18) {
System.out.println("Not eligible");
return;
}
System.out.println("Eligible");
}
public static void main(String[] args) {
checkAge(15);
checkAge(20);
}
}
Output:
Not eligible Eligible
checkAge() has return type void.18, it prints Not eligible.return; stops the method immediately.18 or more, Java skips the if block and prints Eligible.
A method can return a value using return value;.
The returned value must match the method return type.
public class ReturnValueExample {
static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int result = add(10, 20);
System.out.println("Sum = " + result);
}
}
Output:
Sum = 30
add() receives two numbers.a + b.return statement.result.Early return means stopping a method early when a certain condition is met. It helps reduce unnecessary code execution.
public class EarlyReturnExample {
static void login(String username) {
if (username == null) {
System.out.println("Username is required");
return;
}
System.out.println("Welcome, " + username);
}
public static void main(String[] args) {
login(null);
login("Ayan");
}
}
Output:
Username is required Welcome, Ayan
A method can return different values based on conditions.
public class PassFailReturn {
static String checkResult(int marks) {
if (marks >= 33) {
return "Pass";
} else {
return "Fail";
}
}
public static void main(String[] args) {
System.out.println(checkResult(75));
System.out.println(checkResult(20));
}
}
Output:
Pass Fail
A return statement can also return an expression. Java evaluates the expression first, then returns the result.
public class ReturnExpressionExample {
static int square(int number) {
return number * number;
}
public static void main(String[] args) {
System.out.println(square(6));
}
}
Output:
36
6.number * number becomes 6 * 6.36 is returned to the caller.A method can return arrays and objects also, not only primitive values.
public class ReturnArrayExample {
static int[] getMarks() {
return new int[] {80, 90, 75};
}
public static void main(String[] args) {
int[] marks = getMarks();
for (int mark : marks) {
System.out.println(mark);
}
}
}
Output:
80 90 75
Any statement written immediately after return in the same block becomes unreachable.
Java does not allow unreachable code.
public class UnreachableCodeExample {
static int getNumber() {
return 10;
// System.out.println("This line is unreachable");
}
public static void main(String[] args) {
System.out.println(getNumber());
}
}
return 10;, Java will show an
unreachable statement compilation error.
| Statement | What it does | Common use |
|---|---|---|
return |
Stops the current method. | Return a value or exit a method early. |
break |
Stops the nearest loop or switch. | Exit loop or stop switch fall-through. |
continue |
Skips current loop iteration. | Ignore selected values and continue looping. |
void method.return.return with break or continue.What is the difference between return, break, and continue in Java?
return statement stops the current method.return; is used in void methods.return value; is used in non-void methods.return in the same block becomes unreachable.🧠 Test your understanding with a quick quiz
Topic: Return | Language: Java
static void show() {
return 10;
}static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
System.out.println(add(10, 20));
}static int getNumber() {
// return statement here
}static String check(int marks) {
if (marks >= 33) {
return "Pass";
}
return "Fail";
}
public static void main(String[] args) {
System.out.println(check(75));
}🎉 Great job! Continue learning Java step by step.
Mark this lesson as completed to track your learning progress. To keep track across devices, please login and save your learning progress.
Discussion
Ask questions, share suggestions, or discuss this lesson.