Quick revision
The short answer
Return Optional when absence is a normal, expected result that the caller must handle, such as findById. Throw an exception when absence means the caller made a mistake or a contract was broken, return an empty collection rather than null or Optional for collection results, and return a default object when a sensible neutral value exists. Return null only in performance-critical or legacy APIs, documented with nullness annotations, and remember that for primitives OptionalInt and its siblings exist but have fewer methods than Optional.
What to remember
In this answer, we saw that the right signal for "no result" depends on what absence means to the caller. Optional fits expected absence, like findById; exceptions fit mistakes and broken contracts, often through a getX method; empty collections and default objects fit when a neutral value is enough; and null is reserved for hot paths and legacy APIs, always annotated. For primitives, OptionalInt and its siblings avoid boxing but lack map and filter, which affects how callers can use the result.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Return Optional when absence is a normal, expected result that the caller must handle, such as findById. Throw an exception when absence means the caller made a mistake or a contract was broken, return an empty collection rather than null or Optional for collection results, and return a default object when a sensible neutral value exists. Return null only in performance-critical or legacy APIs, documented with nullness annotations, and remember that for primitives OptionalInt and its siblings exist but have fewer methods than Optional.
Why this question is asked
Every public API has methods that sometimes have nothing to return, and the way we signal that shapes every caller's code for years. Pick the wrong signal and callers either crash on a surprise null, write try-catch blocks for ordinary situations, or unwrap Optionals that can never be empty. Interviewers ask this design question because the answer shows whether we think about the caller's situation, not only the method's implementation. In this answer, we will recap the four options, then look at when each one fits, with examples, and finish with the special case of primitive results.
1A Quick Recap of the Four Options
When a method cannot produce a normal result, it can return Optional<T>, a container that is either empty or holds a value; return null, the oldest Java convention; throw an exception, which forces control flow away from the normal path; or return a default object, a real value that stands for "nothing", such as an empty list or a Discount.NONE that applies zero discount.
The right choice depends on one question: from the caller's point of view, is "no result" a normal outcome they should plan for, a mistake they made, or something that a neutral value can absorb without anyone noticing?
- Is the result a collection?YesReturn an empty collectionNoContinue
- Is absence the caller's mistake?YesThrow an exceptionNoContinue
- Is there a neutral default value?YesReturn the default objectNoContinue
- Hot path or legacy API?YesReturn @Nullable TNoReturn Optional<T>
4 steps in order. Play them, or step through them one at a time.
Each question removes one option; most lookup methods end at Optional.
2Return Optional When Absence Is Expected
Optional fits when a caller asks a question whose honest answer is sometimes "none", and the caller must decide what to do. Looking up a user by an id that came from an external request is the classic case: the user may have been deleted, and the caller is in the best position to choose between a 404, a default, or creating a new user.
The type forces the caller to face the empty case, which a @Nullable return only suggests. That is the main advantage of Optional: absence is part of the signature, and the compiler will not let the caller treat Optional<User> as User.
3Throw When Absence Means a Mistake
Sometimes there is no valid reason for the result to be missing. If a caller asks for the account they created a moment ago, or passes an id that our own system generated and guarantees, absence means a bug or broken data. Returning Optional would push a meaningless decision onto every caller, who would mostly write orElseThrow anyway.
A common convention captures both cases with naming: findX returns Optional for expected absence, and getX returns T and throws when it is missing. Callers choose the method that matches their situation. Exceptions are also right when arguments are invalid, such as a negative quantity, because that is the caller breaking the contract rather than the data being absent.
4Return Empty Collections, Never null or Optional
For methods that return a list, set, map or stream, the empty collection already means "nothing". Callers can loop over it, call size() or stream it without any check. Returning null forces a check everywhere and Optional<List<T>> creates two kinds of empty, so both are worse.
Default objects generalise this idea. If an order without a coupon simply has no discount, returning Discount.NONE, whose apply method changes nothing, lets callers run the same code in both cases. The cost is that absence becomes invisible, so default objects fit only where the caller genuinely does not need to know.
5Return null Only Where It Earns Its Place
null still has a role. In a hot path called millions of times per second, allocating an Optional for each call may matter, although escape analysis often removes it; measure before deciding. And in existing APIs, changing null to Optional breaks every caller. The JDK itself keeps Map.get returning null for both reasons.
When we do return null, the signature should say so with a nullness annotation, ideally from one agreed set such as JSpecify's @Nullable, so that IDEs, NullAway and the Kotlin compiler can warn callers who forget to check. An undocumented null is the one option that is never acceptable in a new public API.
6Primitive Results and OptionalInt
For int, long and double results, the JDK offers OptionalInt, OptionalLong and OptionalDouble, which avoid boxing the value into an Integer, Long or Double. Streams return them from methods such as IntStream.max(). But they have fewer methods than Optional: they offer getAsInt, orElse, orElseGet, orElseThrow, ifPresent and isPresent, and no map, filter or flatMap.
So for a public API returning a number that may be missing, think about how callers will use it. If they mostly need a fallback, OptionalInt is fine. If they will transform it, Optional<Integer> may be friendlier despite the boxing, and sometimes a clearer design avoids the question entirely, for example returning a count that is 0 rather than an absent value.
7Follow-up questions
Should a method ever accept Optional as a parameter?
Generally no. Optional is a return type. For an optional argument, overloads, a builder or a @Nullable parameter are clearer, and they do not force every caller to wrap values.
Is getX throwing an exception better for performance than Optional?
Not meaningfully in most code. Exceptions are expensive when thrown, so they should be rare, which is exactly why they fit the "this should not happen" case. If absence is common, throwing on every miss would be both slow and misleading, which is another reason to use Optional there.
What about Kotlin callers of this library?
Kotlin handles @Nullable returns naturally as nullable types and needs a conversion such as getOrNull() for Optional. If most callers are Kotlin services, a well-annotated @Nullable return is often the friendlier choice.
8Conclusion
In this answer, we saw that the right signal for "no result" depends on what absence means to the caller. Optional fits expected absence, like findById; exceptions fit mistakes and broken contracts, often through a getX method; empty collections and default objects fit when a neutral value is enough; and null is reserved for hot paths and legacy APIs, always annotated. For primitives, OptionalInt and its siblings avoid boxing but lack map and filter, which affects how callers can use the result.
Open with
I decide based on what absence means to the caller. If it's a normal outcome they should handle, like looking up a user by an id from a request, I return Optional, because the type forces the caller to deal with the empty case.
Then, if they want more
If absence means the caller made a mistake or our data is broken, I throw. A common pattern is findX returning Optional and getX returning the value and throwing when it's missing, so callers pick the one that fits.
For collections I always return an empty collection, never null and never Optional of a list, because empty already means nothing and callers can loop without checks. Similarly, if there's a neutral default object, like a Discount.NONE that changes nothing, I return that when the caller doesn't need to know.
I return null only in performance-critical code or in existing APIs where changing it would break callers, like Map.get, and then it must be annotated @Nullable so tools and Kotlin can see it.
For primitive results there are OptionalInt, OptionalLong and OptionalDouble, which avoid boxing but have no map, filter or flatMap, so I consider how callers will use the value before choosing them.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- java.util.stream, package documentation, java.util.function, package documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "You are designing a public library API. When should a method return Optional<T>, return...": https://scrivio-interview-library.netlify.app/java/optional/66b8317f5a/