Quick revision
The short answer
I would set one convention: public lookup methods where absence is expected return Optional, every other nullable value uses one nullness annotation set that tools and Kotlin can read, and no method that returns Optional or a collection ever returns null. Static analysis such as Error Prone with NullAway, plus IDE inspections, enforces it in CI. New APIs follow it immediately, and existing APIs change only in their next major version, so no team is broken by the cleanup.
What to remember
In this answer, we saw that mixing Optional, annotated nulls and unannotated nulls costs developers time and gives Kotlin services platform types with no protection. The single convention is Optional for public lookups where absence is expected, one annotation set such as JSpecify for every other nullable value, and never null from methods returning Optional or collections. Error Prone with NullAway, IDE inspections and API review enforce it, and the rollout starts with new APIs and annotations, leaving return type changes to major versions so nobody is broken.
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.
I would set one convention: public lookup methods where absence is expected return Optional, every other nullable value uses one nullness annotation set that tools and Kotlin can read, and no method that returns Optional or a collection ever returns null. Static analysis such as Error Prone with NullAway, plus IDE inspections, enforces it in CI. New APIs follow it immediately, and existing APIs change only in their next major version, so no team is broken by the cleanup.
Why this question is asked
Mixed conventions are expensive in a way that is hard to see. A developer calling an internal library cannot tell from habit whether a missing value comes back as an Optional, a null or an exception, so they read the source or guess, and Kotlin teams get a mixture of Optionals and platform types with no null information at all. Interviewers ask this question to see whether we can choose one convention, explain it in terms everyone accepts, and roll it out without stopping the teams that depend on the libraries. In this answer, we will look at what the mixture costs, state the convention, explain why each part is there, cover enforcement, and plan the migration.
1What the Current Mixture Costs
Consider two internal libraries used by the same Kotlin service. One returns Optional, the other returns null with an annotation from an old set the Kotlin compiler does not read.
Notice that the Kotlin developer had to learn two styles for the same idea, and the second one gives no protection at all. Multiply this by dozens of libraries and hundreds of developers, and the cost shows up as production NullPointerExceptions and slower onboarding, not as one obvious failure.
2The Convention in Three Rules
Rule one: public lookup methods where absence is a normal outcome return Optional<T>. This keeps the strongest signal, a type the Java compiler forces callers to unwrap, for the case where it adds the most value.
Rule two: every other value that may be absent, such as fields, parameters and non-lookup returns, uses one nullness annotation set, and I would pick JSpecify, applied with @NullMarked at package level so non-null is the default. It is the set that NullAway, IntelliJ and the Kotlin compiler all understand, which makes Kotlin see String? instead of a platform type.
Rule three: a method that returns Optional or a collection never returns null. These return types are promises to the caller that a value is always there to call methods on; breaking that promise makes careful callers crash, which is the worst kind of bug.
| Situation | Write | Never |
|---|---|---|
| Lookup that may find nothing | Optional<T> findX(...) | return null |
| Many results | List<T>, empty when none | null, Optional<List<T>> |
| Other nullable return | @Nullable T | Unannotated null |
| Nullable field or parameter | @Nullable T | Optional<T> |
| Everything else | Non-null by @NullMarked | Per-method annotations |
3Why Not One Tool for Everything?
It is tempting to pick a single mechanism. "Optional everywhere" fails because Optional is not designed for fields or parameters, is awkward for Kotlin callers, and cannot stop a null from being passed. "Annotations everywhere, no Optional" is defensible, and some organisations choose it, but it loses the compile-time push that Optional gives Java callers of lookup methods, where forgetting the empty case is the most common bug.
So the split is not a compromise for its own sake; each tool is used where it is strongest. Kotlin teams are served by both: annotated values become nullable types automatically, and Optional returns are converted once at the boundary with getOrNull().
4Enforcing It in CI
The convention is only real if the build checks it. In every Java library, Error Prone with NullAway in JSpecify mode fails the build when a @NullMarked package returns or dereferences an unannotated null, which covers rules two and three. An IDE inspection set, shared through the repository settings, flags the same issues while developers type, and running the same inspections in CI keeps them honest.
Rule one, Optional only for lookups, is partly a judgement call, so API review covers it. A small ArchUnit test can still catch the mechanical parts, such as Optional parameters or fields, and a check that no method returning a collection is annotated @Nullable.
5Rolling It Out Without Breaking Teams
Changing a return type from T to Optional<T>, or from Optional to @Nullable T, breaks every compiled caller, so existing APIs cannot simply be fixed. New APIs follow the convention from day one. Annotations come next, because adding @Nullable to an existing method does not change its bytecode signature and is safe in a minor version; it gives Kotlin and the checkers precise information immediately. Return type changes wait for each library's next major version, with the new method added alongside and the old one deprecated in the minor versions before.
- New APIs follow the conventionfrom the day it is published
- Add JSpecify to existing librariessafe in a minor release
- Enable NullAway in library buildsbaseline old findings
- Add replacement methods, deprecate oldminor releases
- Remove deprecated signaturesnext major version
5 steps in order. Play them, or step through them one at a time.
6Follow-up questions
Why is adding annotations safe in a minor version but changing return types is not?
Annotations are metadata. They do not change the method descriptor, so compiled callers keep linking to the same method. They can produce new warnings or errors when consumers recompile with strict checking, which is a wanted effect, but nothing breaks at runtime. Changing a return type changes the descriptor and breaks compiled callers with NoSuchMethodError.
How do you get twenty teams to accept the convention?
I would write it as a short decision record with the reasoning and examples, share a draft with the library owners and the Kotlin teams before deciding, and adjust it based on their objections. People follow rules they helped shape, and the Kotlin teams in particular gain the most from it, so they tend to become its strongest supporters.
What about third-party libraries that do not follow it?
We cannot change them, so we wrap them at the boundary: an adapter in our code converts their nulls to Optional or annotated types. That keeps the convention consistent inside our code base even when dependencies are not.
7Conclusion
In this answer, we saw that mixing Optional, annotated nulls and unannotated nulls costs developers time and gives Kotlin services platform types with no protection. The single convention is Optional for public lookups where absence is expected, one annotation set such as JSpecify for every other nullable value, and never null from methods returning Optional or collections. Error Prone with NullAway, IDE inspections and API review enforce it, and the rollout starts with new APIs and annotations, leaving return type changes to major versions so nobody is broken.
Open with
I'd set one convention with three rules. First, public lookup methods where absence is a normal outcome return Optional, because that forces Java callers to handle the empty case where they most often forget.
Then, if they want more
Second, every other value that can be absent uses one annotation set, JSpecify, with NullMarked on the package so non-null is the default. That's what NullAway, IntelliJ and the Kotlin compiler all read, so Kotlin teams get real nullable types instead of platform types, which is where their crashes come from today.
Third, a method returning Optional or a collection never returns null, because those types promise the caller there's always something to call methods on. I wouldn't pick a single tool for everything: Optional doesn't fit fields, parameters or Kotlin, and dropping it would lose the compile-time push on lookups.
Enforcement is Error Prone with NullAway in JSpecify mode in every library build, shared IDE inspections, an ArchUnit test against Optional fields and parameters, and API review for the judgement calls.
For rollout, new APIs follow it immediately, annotations get added to existing libraries in minor versions since they don't change bytecode signatures, and any return type change waits for a major version with a deprecated old method alongside, so no team breaks.
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 "Across your organisation, some libraries return Optional, some return null with...": https://scrivio-interview-library.netlify.app/java/optional/c8d67def60/