Return values & void in Java
return ends the method; every path must return in non-void methods.
Handing back an answer
The header declares what a method gives back: int, String... **return value; hands that value to the caller and ends the method immediately**. Any code after it in that path doesn't run. The caller may use the result, or simply ignore it.
static int doubled(int x) {
return x * 2;
}
int y = doubled(4); // y is 8
doubled(4); // legal: result ignoredYour turn
What prints?
static int check(int n) {
if (n < 0) return -1;
System.out.print("ok ");
return n;
}
void main() {
System.out.println(check(-5));
System.out.println(check(3));
}ok -1 ok 3-1 ok 3-1 ok 3
Show the answer
For -5, return -1 ends the method before the print. For 3, the print runs (ok with no newline) and then 3 is returned, so println shows ok 3 on one line.
void: nothing to give
A **void method returns no value. It can still use a bare return; to exit early. But a void call is not a value**: println(log("hi")) won't compile ('void' type not allowed here). The reverse is also an error: a bare return; in an int method (missing return value).
static void log(String m) {
if (m.isEmpty()) return; // exit early
System.out.println(m);
}
static int bad() {
return; // error: missing return value
}Every path must return
In a non-void method, every possible path must end in a return (or a throw). The compiler doesn't reason about your conditions: it sees if ... else if ... and imagines a path where neither is true, with no return. Use a plain else.
static String grade(int s) {
if (s >= 50) {
return "pass";
} else if (s < 50) {
return "fail";
}
} // error: missing return statementYour turn
Does this compile and run?
static String sign(int n) {
if (n > 0) {
return "+";
} else if (n <= 0) {
return "-";
}
}
void main() {
System.out.println(sign(5));
}Prints +Prints -Compile error
Show the answer
Compile error: missing return statement. n > 0 and n <= 0 cover everything, but the compiler doesn't analyze conditions like that. Replace else if (n <= 0) with a plain else.
Return or throw
Can a non-void method end a path with throw instead of return?
Think about it, then reveal the answer
Yes. A path may end in a return or a throw. static int f() { throw new IllegalStateException(); } compiles: no path "falls off the end" without a value.
In real projects
A popular style is the guard clause: return early at the top of a method when the input is invalid (if (user == null) return;), so the main logic isn't buried inside nested ifs. Because return ends the method instantly, the rest can assume the input is fine.
Key takeaways
- return ends the method right away
- A void method may use a bare return; to exit early
- Every path through a non-void method must return a value
- The caller may ignore a returned value
static int f() { while (true) { } } compiles with no return at all! The loop can never finish, so there's no path that reaches the end without a value.
Practice questions
What does this print?
static int check(int n) {
if (n > 10) return 1;
System.out.print("small ");
return 0;
}
void main() {
System.out.println(check(20));
System.out.println(check(5));
}- small 1 small 0
- 1 0
- 1 small 0
- 1 small 0
Check your answer
1 small 0. For 20, return 1 ends the method before the print. For 5, the print runs and then 0 is returned, so println shows "small 0" on one line.
What does this print?
static void log(String m) {
System.out.println(m);
}
void main() {
System.out.println(log("hi"));
}- hi
- hi null
- Compile error
- hi hi
Check your answer
Compile error. log returns void, which isn't a value, so it can't be passed to println: 'void' type not allowed here.