Member-only story
Know how the Java annotations you use work. Part II: Null safety annotations
Null safety is a big concern for Java developers. There’s no point rehashing the regrets of the “inventor” of null, it’s something we must deal with if we want to write programs using Java.
Luckily, there’s an annotation for that. Actually, there are a few different annotations that we can use to specify something must not be null or that it may be null.
The most famous is probably Lombok’s @NonNull
. There’s also @NotNull
from JetBrains, which also provides @Nullable
. The JetBrains annotations are made specifically for use with IntelliJ IDEA, the Java integrated development environment (IDE) from JetBrains.
In Part I of this series, I wrote about how so many Java developers use the annotations in the Spring framework without really understanding how they work. This is, to a lesser extent, also true of annotations from other sources, like the null safety annotations I’ll be going over in this part.
In the previous part, I explained that annotations by themselves don’t actually do anything besides annotate. It is something else in the framework that acts on what the annotation suggests should be done. The compiler, the IDE and other tools can also act on annotations.