Java Return Statement - Return Values from Methods

⏱️ 7 min read • Beginner Level • Lesson 32

Lesson 32 of 124 of Java Tutorial
You have completed this lesson

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.


What is return Statement in Java?

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.

Syntax of return Statement

There are two common forms of return statement:

ReturnSyntax.java
Copy Download
return;        // used in void methods

return value;  // used in methods that return a value
Note: If a method return type is 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.

return in void Method

In a void method, return; is used to stop the method early. It does not return any value.

ReturnInVoidMethod.java
Copy Try Download
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

Explanation

  1. The method checkAge() has return type void.
  2. If age is less than 18, it prints Not eligible.
  3. Then return; stops the method immediately.
  4. If age is 18 or more, Java skips the if block and prints Eligible.

Return Value from Method

A method can return a value using return value;. The returned value must match the method return type.

ReturnValueExample.java
Copy Try Download
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

Explanation

  1. The method add() receives two numbers.
  2. It calculates the sum using a + b.
  3. The result is returned using the return statement.
  4. The returned value is stored in result.
  5. Finally, the value is displayed on the screen.

Early return Example

Early return means stopping a method early when a certain condition is met. It helps reduce unnecessary code execution.

EarlyReturnExample.java
Copy Try Download
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

return with if-else

A method can return different values based on conditions.

PassFailReturn.java
Copy Try Download
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

Return Expression from Method

A return statement can also return an expression. Java evaluates the expression first, then returns the result.

ReturnExpressionExample.java
Copy Try Download
public class ReturnExpressionExample {
    static int square(int number) {
        return number * number;
    }

    public static void main(String[] args) {
        System.out.println(square(6));
    }
}

Output:

36

Explanation

  1. The method receives the value 6.
  2. The expression number * number becomes 6 * 6.
  3. Java evaluates the expression first.
  4. The result 36 is returned to the caller.

Return Array from Method

A method can return arrays and objects also, not only primitive values.

ReturnArrayExample.java
Copy Try Download
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

Unreachable Code after return

Any statement written immediately after return in the same block becomes unreachable. Java does not allow unreachable code.

UnreachableCodeExample.java
Copy Try Download
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());
    }
}
Important: If you uncomment the statement after return 10;, Java will show an unreachable statement compilation error.

Difference Between return, break and continue

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.

Real-World Uses of return

  • Returning calculation results.
  • Returning database records.
  • Returning API responses.
  • Validating user input.
  • Stopping a method when an error occurs.

Common Mistakes with return

  • Returning a value from a void method.
  • Forgetting to return a value from a non-void method.
  • Returning the wrong data type.
  • Writing unreachable code after return.
  • Confusing return with break or continue.
Interview Question:

What is the difference between return, break, and continue in Java?

  • return exits the current method.
  • break exits the loop or switch.
  • continue skips the current loop iteration.
Summary:
  • The return statement stops the current method.
  • It can return a value from a method.
  • return; is used in void methods.
  • return value; is used in non-void methods.
  • The returned value must match the method return type.
  • Code written after return in the same block becomes unreachable.

Interview Questions ⭐

The return statement in Java is used to stop the current method and optionally return a value to the caller.

Yes, return can be used in a void method without a value, like return;, to stop the method early.

No, a void method cannot return a value. It can only use return; without a value.

After return executes, the current method stops immediately and control goes back to the method caller.

Unreachable code means statements written after a return statement in the same block. Java does not allow such code because it can never execute.

return stops the current method, while break stops the nearest loop or switch statement.

Next step: Learn Java Methods

🚀 Continue to Java Methods →

🧠 Test your understanding with a quick quiz



🚀 Quick Knowledge Check

Topic: Return | Language: Java

Question 1 of 10
Q1. Can a method return an array in Java?
Q2. What is wrong with this code?
static void show() {
    return 10;
}
Q3. What is the difference between return and break?
Q4. Which statement is valid inside a void method?
Q5. What will be the output of this code?
static int add(int a, int b) {
    return a + b;
}

public static void main(String[] args) {
    System.out.println(add(10, 20));
}
Q6. Which return statement is correct for a method declared as int?
static int getNumber() {
    // return statement here
}
Q7. What happens when return executes inside a method?
Q8. What is the main purpose of the return statement in Java?
Q9. What is unreachable code in Java?
Q10. What will be the output?
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.

Discussion

Ask questions, share suggestions, or discuss this lesson.

Please login or create an account to join the discussion and save your learning activity.

Loading comments...

Have you completed this lesson?

Mark this lesson as completed to track your learning progress. To keep track across devices, please login and save your learning progress.

Not completed yet.