3.4 Return Types
Return Values in Java Methods
Unit 3: Methods, Worlds, and Constructors · Lesson 3.4 · about 30 minutes · no coding experience needed
Some methods do a job. Others answer a question, and an answer is no use unless it comes back to you. That is what return is for, and Java is unusually strict about it.
What you will be able to do
- Write a method that returns a value
- Use a returned value in a condition or a calculation
- Explain why every path through the method must return
Words you will need
- Return type
- In plain words What kind of answer comes back.
- More precisely The type written before the method name.
- return
- In plain words Hand this value back and stop the method now.
- More precisely A statement that terminates the method and supplies its value.
Build it step by step
Every step below leaves a scenario that compiles and runs. If you stop halfway you will have something that works, not something broken. Follow along in Greenfoot rather than reading straight through.
-
Step 1
On your screen A method with an int return type and a return statement, with both highlighted.
public int doubled(int n) { return n * 2; }`int` before the name is the promise: this method WILL hand back a whole number.
`return n * 2;` keeps the promise. It works out n * 2 and sends it back to whoever called.
The type before the name and the value after return have to agree. Promise an int, return an int.
-
Step 2
On your screen The returned value being stored and used at the call site.
int result = doubled(5); // result is 10 move(doubled(2)); // moves 4Two ways to use the answer, exactly as in 1.5. Store it in a variable, or drop the call straight into another expression.
If you call `doubled(5);` on its own line and do nothing with the result, the method runs and the answer is thrown away. That is legal and almost always a mistake.
-
Step 3
On your screen A boolean-returning method used directly inside an if condition.
public boolean isSafe() { return !isTouching(Spike.class); } if (isSafe()) { move(3); }A boolean return is the most useful kind in this course, because it slots straight into an if.
And look at what it did to the calling code: `if (isSafe())` says what it means. The detail of what "safe" means lives in one place, and if the definition changes you change it once.
-
Step 4
On your screen A method with a return inside an if and no return afterward, with the missing return statement error shown.
// WRONG. Java: missing return statement public int speed() { if (hasBoost) { return 8; } // if hasBoost is false, we fall off the end with no value }Java checks EVERY possible route through the method, and if any route can reach the end without returning, it refuses to compile.
Here, when hasBoost is false, nothing is returned. The fix is either an else with its own return, or a plain `return 4;` after the if as the default.
This strictness is a gift. The alternative is a method that sometimes hands back nothing, and you finding out at the worst moment.
Mistakes almost everyone makes here
These are the wrong ideas students actually build at this point. Read them even if you think you understand, because a wrong idea you have not noticed is the expensive kind.
Common wrong idea return prints the value.
What is actually true It hands the value back to the caller. Nothing appears on screen.
Why it matters Students expecting output conclude the method is broken when it is working perfectly.
Common wrong idea Code after a return still runs.
What is actually true return stops the method immediately.
Why it matters This is what makes an early return a useful tool rather than a bug.
Common wrong idea One return inside an if is enough.
What is actually true Every path must return, or Java refuses to compile.
Why it matters This is the exact cause of "missing return statement", which names no line you consider wrong.
Common wrong idea A void method can return a value.
What is actually true void promises nothing back, so returning a value is an error.
Why it matters Mixing these up produces two confusing errors depending on which way round you got it.
Check your understanding
Answer these before moving on. They are graded instantly and you can retry.
-
1. What does `return n * 2;` do?
-
2. Which return type lets a method be used directly as `if (thing())`?
-
3. Why does this give "missing return statement"? public int f(int n) { if (n > 0) { return 1; } }
-
4. What happens to code written after a return that has just executed?
-
5. `doubled(5);` on a line by itself. What happens to the answer?
Fill in the code
Write a method that reports whether the player still has lives left. It should be usable directly inside an if.
public isAlive()
{
lives > 0;
}
Stuck? Open a hint for each blank
- Blank 1: The answer is a yes or no, so what type comes back?
- Blank 2: The keyword that hands a value back.
Lesson quiz
This one counts toward your progress. Take it when the section above makes sense.
-
1. The type written before a method name is:
-
2. Which method definition compiles?
-
3. What is the advantage of `if (isSafe())` over writing the full condition inline?
-
4. A void method contains `return 5;`. What happens?
The short version
- The type before the name is what comes back. void means nothing.
- return hands a value back AND stops the method immediately.
- Every path through the method must return, or it will not compile.
- A boolean-returning method slots straight into an if.
If you get stuck
These pages cover the problems that come up most often in this lesson. Opening one is not cheating and it is not counted against you.
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]