Scrivio Interview library
LeadershipPrincipal7 min read · updated 12 November 2026

Optional, nullness annotations and Kotlin's nullable types all address the same problem in different ways. For a company with Java and Kotlin services sharing libraries, what long-term null-handling strategy do you choose, and how do you keep it consistent across languages?

I would make nullness annotations the foundation: every shared Java library is annotated with one standard set, JSpecify, with non-null as the default, so Kotlin sees precise types instead of platform types. Optional stays a narrow tool for Java return values where absence is expected, Kotlin code converts it to nullable types at the edge, and APIs written mainly for Kotlin callers avoid Optional altogether. Checkers in the Java build and strict compiler settings in Kotlin enforce the rule, and the conventions are chosen so that a future move to language-level null markers can be done by automated migration.

Why this question is asked

In a company where Java and Kotlin services share libraries, null handling is not a matter of taste. A Java method that returns null without saying so becomes a crash inside a Kotlin service that believed the value was safe, and a Kotlin library that exposes nullable types looks to Java callers like any other reference that might or might not be null. Interviewers ask this question at a senior level because the answer has to cover three different tools, two compilers and many teams, and it has to last for years. In this answer, we will recap what each tool really does, see why platform types are the actual risk, choose one annotation set, decide where Optional still fits, look at how to enforce the rules in both languages, and prepare for null markers in the Java language itself.

1A Quick Recap of the Three Tools

The three tools look similar but live at different levels. Optional is a library class: a small box that holds either one value or nothing. It only helps where a method chooses to return it, and the type system still allows the Optional reference itself to be null. Nullness annotations such as @Nullable are metadata on a type: the Java compiler ignores them, but static checkers, IDEs and the Kotlin compiler read them. Kotlin's nullable types are part of Kotlin's type system: String and String? are different types, and the compiler refuses to let us call a method on a String? without handling null first.

So only Kotlin enforces null safety in its compiler; Java gets the same protection only when annotations exist and a checker reads them.

Three ways to express absence
ToolWhere it livesWho enforces it
Optional<T>Java library type, return valuesNobody; the caller must unwrap it
@Nullable / @NullMarkedAnnotations on Java typesNullAway, IDEs, the Kotlin compiler
String? in KotlinKotlin type systemThe Kotlin compiler, always
Annotations are the only tool both compilers can read, which is why they form the foundation.

2Why Platform Types Are the Real Risk

When Kotlin calls a Java method that has no nullness information, it cannot know whether the result may be null. It gives the result a platform type, shown in the IDE as String!, and lets us treat it either as String or as String?. The compiler is silent either way, so the decision is left to the developer, who usually guesses non-null.

Java
// Java, shared library, no annotations
public class CustomerDirectory {
public String findEmail(String customerId) {
return store.get(customerId); // returns null for an unknown customer
}
}
// Kotlin service
val email: String = directory.findEmail("c-42") // compiles: platform type String!
println(email.length) // NullPointerException at runtime

Notice that the crash happens in Kotlin code that looks perfectly null-safe. The Kotlin team did nothing wrong in their own language; the missing information came from the Java side. In a company with dozens of shared Java libraries, these unannotated return values are where most null bugs in Kotlin services come from.

3Choosing One Annotation Set

The first decision is to annotate Java libraries with exactly one standard set. Over the years teams have used JSR 305, JetBrains annotations, the Checker Framework, Spring's own annotations and several others. Each tool reads a different subset, and their meanings differ in small ways, so mixing them gives inconsistent results. I would choose JSpecify, which reached 1.0 in 2024 as a common standard agreed by tool and library vendors. Spring Framework 7 uses it, NullAway supports it, and recent Kotlin compilers understand it.

The second decision is the default. Annotating every non-null type would be noise, because most references are never null. With @NullMarked on a package or module, every type is non-null unless it says @Nullable, so we only annotate the exceptions.

Java
// package-info.java
@NullMarked
package com.acme.customers;
import org.jspecify.annotations.NullMarked;
// CustomerDirectory.java
public class CustomerDirectory {
public @Nullable String findEmail(String customerId) { ... } // Kotlin sees String?
public Customer load(String customerId) { ... } // Kotlin sees Customer
}
// Kotlin service, same call as before
val email: String = directory.findEmail("c-42") // compile error: String? is not String
val email = directory.findEmail("c-42") ?: return // ok

4Where Optional Still Fits

Optional remains useful in Java for return values where absence is a normal, expected result, such as a repository lookup. It forces the Java caller to think about the empty case and offers map, filter and orElseThrow for handling it. I would keep it for exactly that: Java return values of lookup methods, never fields, parameters or collections.

Kotlin code receives these Optionals and should convert them at the edge into nullable types, because Kotlin's own operators (?., ?: and smart casts) do the same job more naturally. The Kotlin standard library offers getOrNull() for this, and Spring Data adds Kotlin extensions such as findByIdOrNull.

Java
// Java repository, used by both languages
Optional<Customer> findById(String id);
// Kotlin caller converts once, at the boundary
import kotlin.jvm.optionals.getOrNull
val customer: Customer? = repository.findById("c-42").getOrNull()
val name = customer?.name ?: "unknown"

For APIs written mainly for Kotlin callers, I would avoid Optional entirely. If the library is in Kotlin, its functions return T?, which Java callers see as an annotated nullable type. If it is a Java library whose main users are Kotlin services, a @Nullable return is friendlier than an Optional that every caller must unwrap. The rule for library authors becomes simple: Optional only when the main audience is Java code.

5Enforcing the Rules in Both Languages

Conventions that nobody checks drift within a year. On the Java side, I would run NullAway as an Error Prone plugin in every build of the shared libraries, in its JSpecify mode, so that returning null from a method not marked @Nullable, or dereferencing a @Nullable value without a check, fails the build.

On the Kotlin side, the compiler already enforces nullable types. What we configure is how strictly it treats Java annotations: the compiler flag -Xjspecify-annotations=strict turns JSpecify information into errors instead of warnings. Newer Kotlin versions use strict mode by default, but I would set it explicitly in the shared build plugin so every service gets the same behaviour; check your Kotlin version for the current default. A shared Gradle or Maven convention plugin carries both settings, so new services inherit them without copying configuration.

Step throughKeeping one null policy across two languages
  1. Java library changeauthor writes @Nullable only where needed
  2. NullAway in the buildfails on unmarked null returns
  3. Publish libraryannotations travel in the bytecode
  4. Kotlin service compilesstrict JSpecify mode
  5. Type mismatch?
    YesFix the call site before mergeNoService builds with precise types

5 steps in order. Play them, or step through them one at a time.

The annotations are written once in Java and checked by both compilers.

6Preparing for Null Markers in the Java Language

OpenJDK work around Project Valhalla is exploring null-restricted and nullable types, written as String! and String?, alongside value classes. This is not part of Java 25, and its final shape may change, so the strategy should be ready for it without depending on it. The good news is that a @NullMarked package with explicit @Nullable exceptions describes exactly the information a language feature would need, so a later migration could be largely mechanical.

So we keep annotation usage regular, avoid custom aliases, and plan an automated rewrite with a tool such as OpenRewrite once the feature is final.

7Follow-up questions

Why not use Optional everywhere so both languages see the same thing?

Because Optional does not fit Kotlin's model and does not cover fields, parameters or generic type arguments. Kotlin developers would have to unwrap every result instead of using ?. and ?:, and Java developers would still pass nulls into parameters. Annotations describe all positions, cost nothing at runtime and are read by both compilers, so they are the better shared language.

What do you do with libraries that already use JSR 305 or JetBrains annotations?

I would not rewrite everything at once. New and actively changed modules move to JSpecify first, with a scripted rewrite of the annotation imports, which is a simple mechanical change because the meanings are close. Older modules move in their next major version. During the transition, the Kotlin compiler can read several annotation sets, so services keep working.

8Conclusion

In this answer, we saw that Optional, annotations and Kotlin's nullable types work at different levels, and that only annotations are understood by both compilers. The strategy is to annotate all shared Java code with one set, JSpecify, with non-null as the default, to keep Optional for Java lookup results and convert it at the Kotlin boundary, to avoid Optional in Kotlin-first APIs, to enforce it all with NullAway and strict Kotlin settings, and to keep the conventions regular so a future language feature can be adopted by automated migration.

What to say in the interviewAbout 80 seconds aloud

Open with

My foundation would be nullness annotations, not Optional, because annotations are the only thing both the Java checkers and the Kotlin compiler read. I would standardise on one set, JSpecify, mark every shared package @NullMarked so non-null is the default, and only annotate the exceptions with @Nullable.

Then, if they want more

That removes platform types, which is where most null crashes in our Kotlin services come from: a Java method returns null, Kotlin sees String with an exclamation mark, and the developer guesses wrong. Optional stays, but narrowly: Java return values of lookup methods where absence is normal, like findById.

Kotlin code converts it once at the boundary with getOrNull and then uses its own operators. If a library is mainly used by Kotlin services, it should not expose Optional at all; it should return a nullable type.

Enforcement goes into a shared build plugin: NullAway with Error Prone in JSpecify mode for Java, and strict JSpecify handling in the Kotlin compiler, so violations fail the build rather than a code review. For the long term, OpenJDK is exploring null-restricted types with Valhalla.

I would not depend on it, but because our annotations are regular and package-level, moving to language markers later should be an automated rewrite with something like OpenRewrite rather than a manual project.

About this answer

Last updated
12 November 2026
Versions it names
Java 25, Spring Framework 7
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.
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, nullness annotations and Kotlin's nullable types all address the same problem...": https://scrivio-interview-library.netlify.app/java/optional/d4cddf19eb/