Java Return Statement

Naveen AnamaneniUpdated · 4 min read

A shop assistant checking your bill can stop at the first problem: "Sorry, this coupon has expired." There's no point checking the rest. return lets a method do the same: it ends the method immediately, and can hand a value back to whoever called it. It's the strongest jump statement: it leaves every loop and block inside the method at once.

return with a Value

A method that promises a result, such as static int ..., must give one back with return:

Gst.java▶ Try it
public class Gst {
    static int gst(int price) {
        return price * 18 / 100;
    }
 
    public static void main(String[] args) {
        int tax = gst(500);
        System.out.println("GST on Rs. 500: Rs. " + tax);
    }
}
Output
GST on Rs. 500: Rs. 90

Line 3 works out the tax and returns it. Line 7 receives that value into tax. The lesson "Methods in Java" covers writing methods in full; this lesson focuses on what return does to the flow of the program.

Return Early

Checking for bad input first and returning straight away keeps the main logic simple and unindented:

Discount.java▶ Try it
public class Discount {
    static int discount(int amount, boolean isMember) {
        if (amount <= 0) {
            return 0;
        }
        if (!isMember) {
            return 0;
        }
        return amount >= 1000 ? 100 : 50;
    }
 
    public static void main(String[] args) {
        System.out.println(discount(1500, true));
        System.out.println(discount(1500, false));
        System.out.println(discount(-20, true));
        System.out.println(discount(600, true));
    }
}
Output
100
0
0
50

How it works

  1. Lines 3 to 5: a bad amount returns 0 immediately; nothing below runs.
  2. Lines 6 to 8: non-members also get 0.
  3. Line 9 is reached only for valid amounts and members.

These early checks are often called guard clauses.

Try it yourself: add a third guard that returns 0 when amount is more than 50,000.

return Inside a Loop

return is the cleanest way to stop a search and give back the answer:

Search.java▶ Try it
public class Search {
    static int positionOf(int[] pnrs, int wanted) {
        for (int i = 0; i < pnrs.length; i++) {
            if (pnrs[i] == wanted) {
                return i;
            }
        }
        return -1;
    }
 
    public static void main(String[] args) {
        int[] pnrs = {4471, 4398, 4512};
        System.out.println(positionOf(pnrs, 4512));
        System.out.println(positionOf(pnrs, 9999));
    }
}
Output
2
-1

The return i; ends both the loop and the method. If the loop finishes without finding anything, line 8 returns -1.

Every Path Must Return

For a method with a return type, the compiler checks that every path ends with a return. If one path can reach the end without one:

Compiler output
Errs7.java:4: error: missing return statement
    }
    ^

In Search above, removing line 8 causes exactly this error: the compiler can't be sure the loop will find the value.

Common Mistakes

Code after return in the same block. It can never run; the compiler reports unreachable statement.

Returning a value from a void method. return 5; in void method gives incompatible types: unexpected return value.

Forgetting the final return after a loop. See "Every Path Must Return" above.

Practice

  1. Easy: Write static boolean isEven(int n) that returns true for even numbers, and test it with 10 and 7.
  2. Harder: Write static String grade(int marks) using early returns instead of an else-if ladder (A for 90+, B for 75+, C for 60+, D for 35+, F otherwise).

Quick check: return

4 questions
1What does return do?
2What does return; do in a void method?
3When does the compiler report missing return statement?
4What are guard clauses?

Key Takeaways

Was this tutorial helpful?

Lesson 66 of 482