Java Return Statement
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:
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);
}
}GST on Rs. 500: Rs. 90Line 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:
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));
}
}100
0
0
50How it works
- Lines 3 to 5: a bad amount returns 0 immediately; nothing below runs.
- Lines 6 to 8: non-members also get 0.
- 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:
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));
}
}2
-1The 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:
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
- Easy: Write
static boolean isEven(int n)that returnstruefor even numbers, and test it with 10 and 7. - 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).
Key Takeaways
Was this tutorial helpful?