29

As of 2023/JDK21, what @Nullable to use in Java?

Many online materials don't even mention which import to use. So it is now (dark) history in what order those annotation appeared.

currentUserGroups

I hope for general answer and from a reputable source (please quote documentation). For JDK21 some article could do as well.

For example javax.annotation.Nullable may be preferred. But at what version was it introduced? (We use JDK17, maybe next year some projects update to JDK21)

13
  • 7
    The @Nullable annotation doesn't do anything by itself. It's up to whatever product uses it to specify how it uses it. For example, IntelliJ IDEA will treat a field or argument as nullable if it has any kind of @Nullable annotation, irrespective of what package it comes from. Other software may be more choosy about which one you need to use. Also, note that the JDK doesn't know anything about this annotation. There is no JDK version that contains javax.annotation.Nullable - it's up to whoever is interested to actually define it. Commented Jul 6, 2023 at 15:47
  • 1
    Does this answer your question? Commented Jul 6, 2023 at 16:01
  • 3
    Also, this is basically a duplicate of this old question, with the caveat that javax annotations are getting phased out in favor of similar jakarta annotations. Commented Jul 6, 2023 at 16:02
  • 11
    Also, it's interesting how the StackOverflow community has changed its attitude over the years. The "duplicate" question has 1294 upvotes and not a single downvote and has stayed open after 12 years, yet this new question was downvoted immediately after posting and closed within the hour. Sometimes it's better to ask a new question as old questions can have grossly outdated answers that can't really be updated easily. Commented Jul 6, 2023 at 16:08
  • 4
    @k314159 please vote for reopen. This question from the begging asks for modern 2023 solution with links to documentation. @ ParSal @ Max Yes, indeed, old question should be referenced. Commented Jul 6, 2023 at 17:14

3 Answers 3

29

JSpecify

Yet another nullness annotation , JSpecify, was created by a group led by Google:

EISOP Team, Google, JetBrains, Meta , Microsoft , Oracle ,PMD Team , Sonar , Square , Uber , VMware .

They aim to create a tool-independent nullness annotation standard.

They list the reasons to use it in their FAQ

  • consensus
  • precisely defined
  • less strict than the awesome checker framework (complex, hard to migrate, false positives)
  • not tool-specific

Now released

The 1.0.0 release arrived 2024-07. A reference implementation is available now. Presumably, various existing tools will be adding support.

See the thorough documentation and guides available at the JSpecify site.

Sign up to request clarification or add additional context in comments.

7 Comments

@KartikChugh: :-) which do you prefer ?
Google, Meta, Microsoft, Oracle, JetBrains and Sonar agreeing on something is a good sign, but I'm sad to see it's still in beta
JSpecify made its 1.0.0 release on Wednesday: blog post, release notes. Check out the documentation of what to consider when thinking about switching to use them.
The forthcoming Spring Framework 7 is deprecating its version of Nullable in favour of JSpecify. 1 down, 14 more to go.
|
11

It seems to me, that jakarta.annotation.Nullable from jakarta.annotation-api is the closest to being "the standard". The old javax.annotation repo links to its sources and as pointed in the comment, the ever popular checker framework will deal with it.

In terms of adoption, it appears in ~110k files on github as of 06/2024 but I guess this number will still grow as more Servlet projects migrate to newer versions of the spec (javax.annotation.N currently matches >1M files, hard to guess however how many of them are Servlet related and how many of those will be migrated).

1 Comment

Available as of Jakarta EE 10 / version 2.1.0 of the jakarta.annotation-api
5
+100

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.

1 Comment

The checkerframework will care for the @Nullable and @NonNull annotations of the other frameworks too, when they exist, and likewise, most checking tools either respect all those annotations or are configurable regarding which annotations to check for, so in the end, it doesn’t really matter.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.