Quick revision
The short answer
Making every field and parameter Optional does not remove nulls; it moves them and adds cost. Optional is not Serializable and was not designed for fields, so JPA and many mappers handle Optional fields poorly, and Optional parameters force every caller to wrap values while still allowing null to be passed for the Optional itself. A better approach keeps nullable fields with a getter that returns Optional where absence is meaningful, uses overloads or nullness annotations for optional parameters, and adds a nullness checker, which protects far more code at much lower cost.
What to remember
In this answer, we saw that turning every field and parameter into Optional causes more problems than it solves. Optional is not Serializable and is not mapped by JPA, so fields become a source of friction and extra allocation, and Optional parameters force wrapping at every call site while still allowing null. Nullable fields with Optional getters, overloads or parameter objects for optional arguments, and nullness annotations with a checker give broader protection at lower cost, and presenting that as a smaller first step keeps the teammate's good goal intact.
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.
Making every field and parameter Optional does not remove nulls; it moves them and adds cost. Optional is not Serializable and was not designed for fields, so JPA and many mappers handle Optional fields poorly, and Optional parameters force every caller to wrap values while still allowing null to be passed for the Optional itself. A better approach keeps nullable fields with a getter that returns Optional where absence is meaningful, uses overloads or nullness annotations for optional parameters, and adds a nullness checker, which protects far more code at much lower cost.
Why this question is asked
Every team that has fought NullPointerExceptions has had someone propose this: make everything Optional and the problem goes away. The intention is good, which is why reviewing it well matters; a review that only says "no" leaves the real problem unsolved. Interviewers use this scenario to see whether we know the practical limits of Optional and whether we can offer a better plan. In this answer, we will look at what the change looks like, go through the problems it causes for fields and for parameters, show the better alternatives, and finish with how to present the review to the teammate.
1What the Proposed Change Looks Like
Suppose the teammate's pull request turns a simple customer entity and its service into this shape.
On the surface the code now shows which values may be missing. But the change touches hundreds of files, and as we will see, the guarantees it seems to give are not real.
2Why Optional Does Not Belong in Fields
Optional was designed as a return type, and its authors deliberately did not make it Serializable. Any class with an Optional field can no longer be serialized with Java serialization, which matters for session replication, some caches and older messaging code.
JPA is the more immediate problem. JPA providers such as Hibernate map basic attribute types like String, numbers and dates; Optional is not one of them, so the entity above fails at startup or needs field access tricks and converters to work at all. JSON mappers are similar: older Jackson versions need an extra module to read and write Optional properly, and many other mapping libraries, such as MapStruct or the persistence layers of some frameworks, treat Optional fields inconsistently. Check your versions, but expect friction.
There is also a memory cost. Each present Optional is an extra object on the heap, roughly 16 bytes plus its header and a reference. For an entity with five optional fields loaded 100,000 times in a batch, that is up to 500,000 extra small objects for the garbage collector to handle, all to say "this may be null", which the field could already say for free.
3Why Optional Parameters Do Not Help Either
Optional parameters look explicit, but they add a burden for callers without removing the problem.
Notice that an Optional parameter now has three states: present, empty and null. Nothing in the language prevents the third, so the method still needs a null check, and we have gained nothing over a plain nullable parameter. Meanwhile every call site is longer and harder to read, and the method signature now forces wrapping even on callers who always have a value.
The better tools for optional parameters are overloads, which give each combination a clear name, a parameter object or builder when there are several optional values, or a plain parameter annotated @Nullable, which a checker can enforce.
4What I Would Suggest Instead
For fields, keep the field nullable and let the getter express absence when callers need to know about it. For parameters, use overloads. For the whole codebase, add nullness annotations and a checker.
| Place | Teammate's proposal | Suggested approach |
|---|---|---|
| Entity field | Optional<String> middleName | @Nullable String, Optional getter |
| Optional parameter | Optional<Discount> discount | Overload or Discount.NONE |
| Several optional inputs | Many Optional parameters | Parameter object or builder |
| Lookup result | Optional<Customer> | Optional<Customer> (keep it) |
| Whole codebase | Wrap everything | @NullMarked packages plus NullAway |
This keeps JPA and serialization happy, removes wrapping at call sites, and still shows the caller of getMiddleName() that the value may be missing. Notice also Discount.NONE, a null object that represents "no discount", which avoids both null and Optional where a sensible neutral value exists.
5Why a Nullness Checker Gives Broader Protection
Optional only protects the places where it appears, and only if every caller uses it properly. Nullness annotations cover every position: fields, parameters, return values and generic type arguments. With JSpecify's @NullMarked on our packages, every type is non-null unless marked @Nullable, and a checker such as NullAway, running in the build through Error Prone, reports any path where a possibly null value is dereferenced or passed where non-null is expected.
So the checker catches the case that Optional parameters miss, passing null where it is not allowed, at compile time and at no runtime cost. It also works without changing the shape of the code, so the pull request shrinks from hundreds of signature changes to adding annotations at the few places where null is really allowed.
6How I Would Give This Review
I would start by agreeing with the goal, because fewer NullPointerExceptions is something the whole team wants, and by pointing out where the pull request is right: lookup methods returning Optional are a good idea and should stay. Then I would explain the concrete problems with fields and parameters using our own code, for example the entity that no longer maps, rather than quoting rules.
Finally I would propose a smaller first step: annotate one module with @NullMarked, turn on NullAway for it, and fix what it finds. That usually finds real bugs in the first day, which makes a much stronger case than any argument, and it gives the teammate ownership of a better version of their own idea.
7Follow-up questions
Is it ever acceptable to have an Optional field?
In a class that is never persisted or serialized, such as a short-lived value computed inside a method, an Optional field does no serious harm. But the general guidance from the JDK designers is to use Optional as a return type, and a nullable field with an Optional getter gives the same clarity without the costs.
Why not just validate parameters with Objects.requireNonNull?
We should, at public boundaries, because it fails fast with a clear message. But it is a runtime check: it finds the problem when the code runs. Annotations with a checker find it at compile time, and the two work well together: annotations document and check the contract, and requireNonNull guards the edges where unchecked code calls in.
Does Spring Data support Optional at all?
Yes, as a return type: repository methods such as findById return Optional, and derived query methods may be declared to return Optional<T>. That is exactly the intended use, and it is a good example to show the teammate where Optional fits.
8Conclusion
In this answer, we saw that turning every field and parameter into Optional causes more problems than it solves. Optional is not Serializable and is not mapped by JPA, so fields become a source of friction and extra allocation, and Optional parameters force wrapping at every call site while still allowing null. Nullable fields with Optional getters, overloads or parameter objects for optional arguments, and nullness annotations with a checker give broader protection at lower cost, and presenting that as a smaller first step keeps the teammate's good goal intact.
Open with
I'd start by agreeing with the goal, fewer null pointer exceptions, and then explain why this way of getting there costs more than it gives. Optional was designed as a return type.
Then, if they want more
It isn't Serializable, JPA providers don't map Optional fields, older Jackson setups need an extra module, and every present Optional is an extra object, so a batch of a hundred thousand entities creates hundreds of thousands of extra allocations.
Parameters are worse in a way: every caller has to wrap values, and the Optional itself can still be null, so the method still needs a null check and we've gained nothing.
What I'd suggest instead is: keep fields nullable and give them a getter that returns Optional when absence matters to callers; use overloads, a parameter object or a null object like Discount.NONE for optional arguments; and keep Optional as the return type of lookup methods, which the teammate got right.
For real protection, I'd put JSpecify's NullMarked on our packages and run NullAway in the build. That covers fields, parameters and returns, catches passing null at compile time, and costs nothing at runtime. I'd propose doing it on one module first, because it usually finds real bugs immediately, which convinces people better than any argument.
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 "A teammate makes every entity field Optional and every method parameter Optional to...": https://scrivio-interview-library.netlify.app/java/optional/9e9085ee0f/