Operators ·
‹ Previous Next ›
⏱ 5 min read Modified: 2026-07-13

Java return Statement

The return statement in Java ends the execution of the current method and hands control back to the caller. If the method is declared with a return type, return also passes a result back — the value the calling code receives.

What the return Statement Does

The return statement has two forms:

return;            // ends the method without returning a value (void only)
return expression; // ends the method and returns the value of the expression

In both cases the method stops immediately: statements placed after return are not executed, and the program continues from the point where the method was called.

Form Where it is used What it does
return; void methods, constructors Exits the method early without a value
return expression; Methods with a declared return type Exits the method and returns a result
No return at all void methods only The method ends after its last statement

return in void Methods: Early Exit

If a method is declared as void — that is, it does not return a value — the return statement is optional: the method ends on its own once its last statement has run. However, return; without a value is handy for leaving a method early, for example when there is no point in continuing:

public class ReturnExample1 {
    public static void main(String[] args) {
        boolean t = true;
        System.out.println("Before return.");
        if (t) {
            return;
        }
        System.out.println("This line will not be executed.");
    }
}

Output of this code:

Before return.

As soon as execution reaches the return inside the if statement, the main method ends, and the second println call never runs. This kind of early, condition-based exit is a common way to validate input at the top of a method: if the data is invalid, the method returns right away and the main logic is skipped.

Important

The return statement ends only the current method, not the whole program. Execution continues from wherever the method was called. To shut down the JVM entirely you would call System.exit(0) — which regular application code almost never needs. In the example above return appears to stop the program only because it sits inside main.

Returning a Value from a Method

If a method declares a return type other than void, the return statement is mandatory. The keyword return is followed by an expression whose value is returned from the method. The type of that expression must be compatible with the type declared in the method signature:

public class ReturnExample2 {
    public static void main(String[] args) {
        double d = getRandomValue(3);
        System.out.println(d);
    }

    public static double getRandomValue(int i) {
        return Math.random() * i;
    }
}

Possible output of this code (the number is random, so it changes on every run):

1.8536012182872652

The method getRandomValue is declared with the type double, so it must return a value of that type along every possible execution path. The expression Math.random() * i is evaluated, and its result is passed back to the call site, where it is assigned to the variable d.

A method may contain several return statements — one per branch of its logic:

public static int sign(int value) {
    if (value > 0) {
        return 1;
    }
    if (value < 0) {
        return -1;
    }
    return 0;
}

The compiler checks that a value is returned no matter which branch runs: remove the final return 0; and the code stops compiling.

Common Compile-Time Errors with return

Unreachable code (unreachable statement). Any statement placed directly after return in the same block can never run, and the compiler reports an error:

public static int square(int x) {
    return x * x;
    // System.out.println("Done"); // compile error: unreachable statement
}

Not every path returns a value (missing return statement). If even one execution path of a method with a return type does not end in a return statement, compilation fails:

public static int sign(int value) {
    if (value > 0) {
        return 1;
    }
    // compile error: missing return statement —
    // when value <= 0 the method has nothing to return
}

Returning a value from a void method. A return statement with a value inside a void method is a compile error ("cannot return a value from method whose result type is void"). In void methods only the bare form return; is allowed.

Mixing up return and break. The break statement terminates only the nearest loop or switch statement, while return terminates the entire method — including any loops running inside it.

Frequently Asked Questions

Can a method have more than one return statement?

Yes, this is allowed and widely used: early exits (guard clauses) at the top of a method often make the code cleaner than a single return buried in nested if-else blocks. The only requirement is that every possible execution path of a method with a return type ends in a return statement or throws an exception.

What is the difference between return and break?

The break statement ends only the nearest loop or switch, after which the method keeps running. The return statement ends the whole method: if it is called inside a loop, it terminates both the loop and the method at once.

What does the compile error "missing return statement" mean?

It means the method is declared with a return type, but there is an execution path that does not end in a return statement. The most frequent cause is an if without an else branch: the compiler cannot guarantee the condition is true, so it demands a return for the case when the condition is false.

Does the finally block run if there is a return in try?

Yes. A return inside a try block first evaluates the value to return, then the finally block runs, and only after that does the method actually end. Putting a return inside finally itself is discouraged: it overrides the value from try and can swallow exceptions.

Comments

Please log in or register to have a possibility to add comment.