Quick revision
The short answer
Optional was added in Java 8 mainly so that stream operations like findFirst and max could say "there is no result" clearly; it was never meant as a general replacement for null. A library type cannot remove null from the type system, because every reference, including an Optional, can still be null. OpenJDK is exploring null-restricted types alongside Valhalla's value classes, so my strategy is to combine Optional returns with nullness annotations and checkers now, and to keep our conventions regular enough to move to language support later with automated changes.
What to remember
In this answer, we saw that Optional was designed as a limited return type, mainly for stream results like findFirst, and that no library type can remove null because every Java reference can hold it. OpenJDK is exploring null-restricted types with value classes, which we should plan for without depending on. Until then, Optional on lookups, one nullness annotation set, a checker in the build and runtime guards at boundaries give most of the safety, and keeping these conventions regular lets us move to language support later with automated changes.
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 was added in Java 8 mainly so that stream operations like findFirst and max could say "there is no result" clearly; it was never meant as a general replacement for null. A library type cannot remove null from the type system, because every reference, including an Optional, can still be null. OpenJDK is exploring null-restricted types alongside Valhalla's value classes, so my strategy is to combine Optional returns with nullness annotations and checkers now, and to keep our conventions regular enough to move to language support later with automated changes.
Why this question is asked
Many teams adopted Optional expecting it to end NullPointerExceptions, and were disappointed when the crashes continued. Understanding why it was designed narrowly explains that disappointment and, more usefully, tells us what will and will not help in the coming years. Interviewers ask this at a principal level because the answer requires history, type-system reasoning and a strategy that survives language changes we cannot yet predict. In this answer, we will look at what Optional was designed for, why no library class can fix null, what OpenJDK is working on, and how to build a strategy that works today and can move to language support later.
1What Optional Was Designed For
Optional arrived with the streams API in Java 8. Streams needed an answer for operations that may have no result: findFirst on an empty stream, max of nothing, reduce of zero elements. Returning null would have been ambiguous and easy to ignore, so the designers added a small container type that is either empty or holds one value, and returned it from those operations.
The JDK designers have explained publicly that they intended Optional as a limited mechanism for method return types where "no result" must be represented clearly. That is why it is not Serializable, why it was not designed for fields or parameters, and why it has no special support in the language. It is a well-made tool for one job, not a redesign of how Java handles absence.
2Why a Library Type Cannot Fix null
The core reason is that null is part of Java's type system. Every reference type, String, Customer or Optional<Customer>, includes null as a possible value, and the compiler does not track which references might hold it. Adding a class to the library does not change that rule.
Notice what each line shows. Optional cannot stop itself from being null, cannot stop callers from passing null where it is expected, and is routinely converted back to null at boundaries. Languages where an option type does remove null, such as Haskell, have no null in the first place, and Kotlin solves it by making nullability part of every type. Java has neither, so Optional can only ever be a signal on the return types that choose to use it.
3What OpenJDK Is Exploring
Real null safety needs the compiler to know which types allow null. OpenJDK has been exploring this as part of Project Valhalla: value classes, objects without identity, are being worked on first, and a related proposal describes null-restricted and nullable types, written as String! and String?, with the compiler checking them and the JVM able to use the information for flatter memory layouts.
None of this is final in Java 25, and the syntax, semantics and timing may all change. So the right stance is to plan for it without depending on it: we should not wait for it, and we should not adopt preview features in production code, but we should make sure our code would be easy to move when it arrives.
4The Strategy Until Then
Until the language helps, we can get most of the benefit from three layers that each cover a different gap. Optional stays as the return type of lookup methods where absence is expected, because it forces Java callers to handle the empty case. Nullness annotations from one standard set, JSpecify, describe every other position, with @NullMarked making non-null the default. And a checker such as NullAway, running in the build, turns those annotations into compile errors, which is the closest thing to language support we have today.
At system boundaries, where unchecked code calls in, Objects.requireNonNull adds a runtime check that fails fast with a clear message. Together these layers cover returns, parameters, fields and boundaries, which Optional alone never could.
- Language null markersfuture: String! and String? checked by javac
- Checker in the buildNullAway turns annotations into errors
- Nullness annotationsJSpecify
@NullMarkedwith@Nullableexceptions - Optional returnslookup methods where absence is expected
- Runtime guards
requireNonNullwhere unchecked code calls in
5 layers, from the top down. Step through them one at a time.
The top layer may one day replace the two below it; the conventions are chosen so that can be automated.
5Conventions That Can Move to the Language
The last part of the strategy is to keep our conventions regular, because regular code can be migrated by tools. If every package declares @NullMarked and every nullable type says @Nullable from one annotation set, a future migration is a mechanical rewrite: @Nullable String becomes String?, and the default becomes whatever the language offers. Tools such as OpenRewrite are built for exactly this kind of change across hundreds of repositories.
Irregular code is what makes such migrations expensive: three annotation sets, custom aliases, Optional parameters in some places and unannotated nulls in others. So the conventions we set today are partly an investment in a change we cannot schedule. I would review them each year against the state of the OpenJDK work, and adopt language support only when it is final, not when it is in preview.
6Follow-up questions
If Optional cannot fix null, why use it at all?
Because on return types it does its one job well: the caller of findById cannot assign the result to a Customer without deciding what happens when it is empty. That compile-time push is valuable on lookups, which are a common source of forgotten null checks. The mistake is expecting it to do more.
Would value classes make Optional cheaper?
That is one of the hopes. Optional is documented as value-based, which means it does not rely on identity, so it could become a value class and avoid heap allocation in many cases. Until that happens, allocation is usually cheap and often removed by escape analysis, so it is rarely a reason to avoid Optional on returns.
Should we adopt Kotlin to get null safety now?
For new services where the team knows Kotlin, it is a reasonable choice, and its null safety is a real benefit. But it is a language decision with hiring, tooling and training costs, and it does not help existing Java code. Annotations and checkers improve the Java code we already have, and they also make Kotlin interop better.
7Conclusion
In this answer, we saw that Optional was designed as a limited return type, mainly for stream results like findFirst, and that no library type can remove null because every Java reference can hold it. OpenJDK is exploring null-restricted types with value classes, which we should plan for without depending on. Until then, Optional on lookups, one nullness annotation set, a checker in the build and runtime guards at boundaries give most of the safety, and keeping these conventions regular lets us move to language support later with automated changes.
Open with
Optional came with streams in Java 8. Operations like findFirst, max and reduce needed a clear way to say there's no result, and the JDK designers have been explicit that they intended it as a limited return-type tool, not a replacement for null.
Then, if they want more
That's why it isn't Serializable and wasn't designed for fields or parameters. And it can't remove null anyway, because null is part of Java's type system: any reference, including an Optional, can be null, callers can pass null where an Optional is expected, and values get turned back into null at boundaries.
Real null safety needs the compiler to know which types allow null. OpenJDK is exploring that with null-restricted types alongside Valhalla's value classes, but it isn't final in Java 25 and could change, so I plan for it without depending on it.
Today I'd layer the tools: Optional for lookup returns, JSpecify annotations with NullMarked packages for everything else, NullAway in the build to turn them into compile errors, and requireNonNull at boundaries.
The key long-term decision is keeping those conventions regular, one annotation set and package-level defaults, so that when the language support arrives we can migrate with automated rewrites like OpenRewrite instead of a manual project across hundreds of repositories.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 8, Java 25
- 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 "Optional was designed as a limited return-type tool, yet many teams expected it to...": https://scrivio-interview-library.netlify.app/java/optional/095d0f280e/