Quick revision
The short answer
Optional makes the possible absence of a return value part of the method signature, so callers cannot forget to handle it the way they forget a null check. Its methods differ in when they run: orElse(value) evaluates its argument every time, even when a value is present; orElseGet(supplier) calls the supplier only when empty, so it suits expensive defaults; orElseThrow() (Java 10) throws NoSuchElementException when empty, and orElseThrow(supplier) throws an exception we choose; or(supplier) (Java 9) returns another Optional when empty, which lets us chain fallbacks.
What to remember
In this answer, we saw that Optional makes the possible absence of a return value explicit in the method signature. orElse evaluates its argument every time, so it is for constants; orElseGet runs its supplier only when empty, so it is for expensive defaults; orElseThrow() from Java 10 throws NoSuchElementException and orElseThrow(supplier) throws our own exception; and or(supplier) from Java 9 chains Optional fallbacks. We also saw that Optional belongs in return types, must never itself be null, and is best used through map and the orElse family rather than isPresent and get.
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.
Optional makes the possible absence of a return value part of the method signature, so callers cannot forget to handle it the way they forget a null check. Its methods differ in when they run: orElse(value) evaluates its argument every time, even when a value is present; orElseGet(supplier) calls the supplier only when empty, so it suits expensive defaults; orElseThrow() (Java 10) throws NoSuchElementException when empty, and orElseThrow(supplier) throws an exception we choose; or(supplier) (Java 9) returns another Optional when empty, which lets us chain fallbacks.
Why this question is asked
NullPointerException has been the most common runtime error in Java for decades, and the root cause is usually not the line that throws but a method that returned null without anyone noticing it could. Optional, added in Java 8, is the standard library's answer for return values. Interviewers ask about it because it is used everywhere, from Spring Data's findById to our own services, and because the differences between its fallback methods cause real bugs. In this answer, we will recap the problem Optional solves, then go through orElse, orElseGet, orElseThrow and or with examples, and finish with guidelines for using Optional well.
1A Quick Recap: The Problem With Returning null
Consider a method User findByEmail(String email). Looking at the signature, we cannot tell whether it returns null when no user matches, throws an exception, or never fails. The answer is in the documentation, if anyone wrote it, or in the implementation. Callers who guess wrong get a NullPointerException, often far from the method that returned null, which makes it hard to trace.
Optional<T> is a container that either holds a value or is empty. A method that returns Optional<User> states, in its type, that there may be no user, and the compiler forces the caller to unwrap it. The caller cannot accidentally call a User method on an empty result, because Optional has no User methods; it has methods for deciding what to do when the value is missing.
2OrElse: A Value That Is Always Evaluated
orElse(other) returns the contained value if present, otherwise other. The catch is that other is an ordinary method argument, and Java evaluates every argument before calling the method. So whatever expression we write inside orElse runs every time, whether the Optional has a value or not.
For a constant like "default" or a value we already have, this does not matter, and orElse is the simplest choice. For anything expensive, such as a database query, a remote call or creating a large object, it means paying that cost on every call, including the common case where the value was present.
3OrElseGet: A Default Computed Only When Needed
orElseGet(supplier) takes a Supplier, a function with no arguments that produces a value, instead of the value itself. Optional calls the supplier only when it is empty.
Notice the difference from orElse. Passing this::fallback hands over the recipe for the default, not the default itself, so nothing runs until it is needed. The rule of thumb is simple: orElse for constants and already-computed values, orElseGet for anything that costs time, has side effects, or allocates.
4OrElseThrow: Absence as an Error
Sometimes absence means something went wrong, such as an id that should exist. orElseThrow expresses that directly. The no-argument form, added in Java 10, throws NoSuchElementException when empty; it does the same as get() but its name makes the intent obvious, which is why it is preferred over get(). The form with a Supplier of an exception lets us throw a meaningful, domain-specific exception.
The exception is also created lazily, by the Supplier, so building its message costs nothing when the user exists.
5Or: Chaining Fallbacks That Are Themselves Optional
or(supplier), added in Java 9, is for the case where the fallback might also be missing. If the Optional has a value, or returns it unchanged. If it is empty, or calls the supplier, which returns another Optional. That lets us try several sources in order and still end with an Optional.
| Method | When empty | Argument evaluated | Since |
|---|---|---|---|
orElse(value) | returns value | always, before the call | Java 8 |
orElseGet(supplier) | returns supplier.get() | only when empty | Java 8 |
orElseThrow() | throws NoSuchElementException | no argument | Java 10 |
orElseThrow(exceptionSupplier) | throws the supplied exception | only when empty | Java 8 |
or(optionalSupplier) | returns another Optional | only when empty | Java 9 |
orElse evaluates its argument when a value is present.Each source is asked only if the earlier ones came back empty, because each fallback is a Supplier. Before Java 9 this needed nested if statements or awkward flatMap tricks.
6Using Optional Well
Optional was designed for return types, where it says "there may be no result". It is a poor fit elsewhere. As a field, it adds an object per instance and Optional is not Serializable. As a method parameter, it makes callers wrap arguments for no benefit; overloading or a nullable parameter with clear documentation is simpler. In collections, an empty collection already expresses absence, so List<Optional<T>> or Optional<List<T>> is usually a sign to rethink.
Two more rules prevent most misuse. A method that returns Optional must never return null itself, or it defeats the purpose. And isPresent() followed by get() is just a null check in disguise; map, flatMap, filter, ifPresent, ifPresentOrElse and the orElse family express the same logic more safely.
7Follow-up questions
Is Optional.get() deprecated?
It is not deprecated, but orElseThrow() was added in Java 10 as the same operation with a clearer name, and style guides and IDE inspections steer people towards it. get() reads as if it always succeeds, which hides the fact that it can throw.
Can orElseGet return null?
Yes. If the supplier returns null, orElseGet returns null, and orElse(null) does the same. That is sometimes used deliberately at the boundary with older APIs that expect null, but inside our own code it reintroduces the problem Optional was meant to remove.
Should getters on entities return Optional?
For a genuinely optional relationship, returning Optional from the getter can make the API clearer, while the field itself stays a plain nullable reference so persistence frameworks and serialisation work normally. For fields that are always set, a plain return type is better.
8Conclusion
In this answer, we saw that Optional makes the possible absence of a return value explicit in the method signature. orElse evaluates its argument every time, so it is for constants; orElseGet runs its supplier only when empty, so it is for expensive defaults; orElseThrow() from Java 10 throws NoSuchElementException and orElseThrow(supplier) throws our own exception; and or(supplier) from Java 9 chains Optional fallbacks. We also saw that Optional belongs in return types, must never itself be null, and is best used through map and the orElse family rather than isPresent and get.
Open with
Optional solves the problem that a method returning null does not say so in its signature, so callers forget to check and get a NullPointerException far from the cause.
Then, if they want more
Returning Optional<User> makes absence part of the type, and the caller has to decide what to do when it is empty.
The fallback methods differ mainly in when they run. orElse takes a value, and because Java evaluates arguments before the call, that expression runs every time, even when the Optional has a value, so I only use it for constants or values I already have. orElseGet takes a Supplier and only calls it when the Optional is empty, so it is the right choice for expensive defaults like a database lookup or a remote call. orElseThrow with no arguments, added in Java 10, throws NoSuchElementException when empty; it is what get() does, but with an honest name. orElseThrow with a supplier lets me throw a domain exception, like UserNotFoundException, which a controller advice can map to a 404.
And or, from Java 9, takes a supplier of another Optional, so I can chain fallbacks like cache, then database, then directory, each asked only if the previous one was empty.
Beyond that, I use Optional for return types, not fields or parameters, never return null from an Optional method, and prefer map and the orElse family over isPresent and get.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 10, Java 9, Java 8
- 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 "What problem does Optional solve, and what are the differences between orElse...": https://scrivio-interview-library.netlify.app/java/optional/e39308d637/