1

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

Many online materials, don't even mention what import should be.
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)

Paul Verest
  • 60,022
  • 51
  • 208
  • 332
  • 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. – k314159 Jul 06 '23 at 15:47
  • Please don't fear to give votable/bountiable answer. – Paul Verest Jul 06 '23 at 15:51
  • 1
    Does [this](https://stackoverflow.com/questions/4963300/which-notnull-java-annotation-should-i-use) answer your question? – ParSal Jul 06 '23 at 16:01
  • 1
    Also, this is basically a duplicate of [this old question](https://stackoverflow.com/q/4963300/341772), with the caveat that javax annotations are getting phased out in favor of similar [jakarta annotations](https://jakarta.ee/specifications/annotations/). – Max Jul 06 '23 at 16:02
  • Eek! We had 14 different types of `@Nullable` annotations, now Jakarta comes along and we have 15. – k314159 Jul 06 '23 at 16:05
  • 5
    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. – k314159 Jul 06 '23 at 16:08
  • Those Jakarta annotations are for web development (Java Enterprise Edition). I'd be surprised if they were in fact being favored over the `javax` ones. – markspace Jul 06 '23 at 16:13
  • 1
    @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. – Paul Verest Jul 06 '23 at 17:14
  • How is this not a library recommendation request? – Charles Duffy Jul 07 '23 at 13:12
  • @markspace anything `javax` has license problems AFAIK, so to my understanding, `jakarta` is aiming to replace it in all possible contexts, not just web apps. – morgwai Aug 25 '23 at 17:19
  • "`javax`" is the package Oracle uses for many Java API classes. It's under the same license as the rest of Java. I don't know what you're thinking of, but there's no license problems with it. https://docs.oracle.com/javase/8/docs/api/javax/annotation/package-summary.html – markspace Aug 25 '23 at 17:25
  • @markspace from https://en.wikipedia.org/wiki/Jakarta_EE#History : "_The Eclipse Foundation could not agree with Oracle over the use of javax and Java trademarks._" I honestly wasn't interested in legal details myself, but I strongly believe that all the package renaming was not "just for fun", but because of important legal reasons. – morgwai Aug 25 '23 at 22:43
  • @markspace maybe my first comment was ambiguous: I did **not** mean that merely **using** official `javax` classes from Oracle is in violation of any license, I meant that any 3rd parties (such as Eclipse Jakarta project) cannot add/modify anything there and thus `javax.annotation.Nullable` (and many others) are in violation (as also stated in the answer below by @mernst) – morgwai Aug 25 '23 at 22:49

2 Answers2

4

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.

mernst
  • 7,437
  • 30
  • 45
  • 1
    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. – Holger Jul 13 '23 at 11:05
2

IMO jakarta.annotation.Nullable from jakarta.annotation-api is the closet 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.

morgwai
  • 2,513
  • 4
  • 25
  • 31