Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What @Nullable to use in Java (as of 2023/JDK21)?

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)

like image 913
Paul Verest Avatar asked Sep 08 '25 09:09

Paul Verest


2 Answers

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.

like image 142
serv-inc Avatar answered Sep 10 '25 02:09

serv-inc


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).

like image 22
morgwai Avatar answered Sep 10 '25 03:09

morgwai