In 2023, the best choice for the @Nullable
and @NonNull
annotation remains that of the Checker Framework.
It is the de facto standard (import org.checkerframework.checker.nullness.qual...
appears in 75k files on GitHub), is recognized by the widest variety of tools, is a superset of most other nullness annotations, and has a clear, standard semantics.
The annotation is useful for a variety of purposes.
You can write it as concise, precise documentation.
You can use a tool to verify or to heuristically check that documentation. Examples include:
The Checker Framework's Nullness Checker is the most comprehensive and can be configured to be sound or lenient. The Checker Framework also supports many other type systems.
NullAway is a simpler tool that supports only nullness checking and that runs faster than the Nullness Checker.
Many others.
Some automated test frameworks respect the annotation when generating tests.
Your question mentions javax.annotation.Nullable
. That annotation is not approved by Oracle and was never authorized to use the javax
namespace (it did so in violation of the Oracle Java binary license). The tool that introduced it, FindBugs, has been abandoned.
There is no official answer to your question that is blessed by Oracle. JSR 305, whose goal was to define such an annotation, was abandoned and (according to my conversation with Mark Reinhold, who is the Chief Architect of the Java Platform Group at Oracle), Oracle is not interested in reviving it. Every @Nullable
annotation is provided by a third-party library and is ignored by javac
. The annotation is read only by third-party tools, such as annotation processors.