432

I saw some method in java declared as:

void foo(@Nullable Object obj)
{ ... }

What's the meaning of @Nullable here? Does it mean the input could be null?

Without the annotation, the input can still be null, so I guess that's not just it?

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
user1508893
  • 9,223
  • 14
  • 45
  • 57
  • 4
    From the answers below, I've learned that the annotation indicates null values are acceptable, examples of where it is used, code analyzers I can try out and that interpretations may vary. See why this format is a good one? +1s all round, especially for asking this question. – icedwater Oct 03 '13 at 04:06
  • 14
    Which library is providing `@Nullable` in the context of your question? Is it [Checker?](https://checkerframework.org/manual/#nullness-checker) – 8bitjunkie Apr 04 '17 at 15:29
  • See https://stackoverflow.com/questions/4963300 – tkruse Jul 28 '18 at 01:17

4 Answers4

447

It makes it clear that the method accepts null values, and that if you override the method, you should also accept null values.

It also serves as a hint for code analyzers like FindBugs. For example, if such a method dereferences its argument without checking for null first, FindBugs will emit a warning.

kevinarpe
  • 20,319
  • 26
  • 127
  • 154
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
95

This annotation is commonly used to eliminate NullPointerExceptions. @Nullable says that this parameter might be null. A good example of such behaviour can be found in Google Guice. In this lightweight dependency injection framework you can tell that this dependency might be null. If you would try to pass null without an annotation the framework would refuse to do it's job.

What is more, @Nullable might be used with @NotNull annotation. Here you can find some tips on how to use them properly. Code inspection in IntelliJ checks the annotations and helps to debug the code.

Mike Rapadas
  • 4,613
  • 2
  • 28
  • 21
Adam Sznajder
  • 9,108
  • 4
  • 39
  • 60
20

Different tools may interpret the meaning of @Nullable differently. For example, the Checker Framework and FindBugs handle @Nullable differently.

mernst
  • 7,437
  • 30
  • 45
reprogrammer
  • 14,298
  • 16
  • 57
  • 93
7

Granted, there are definitely different thinking, in my world, I cannot enforce "Never pass a null" because I am dealing with uncontrollable third parties like API callers, database records, former programmers etc... so I am paranoid and defensive in approaches. Since you are on Java8 or later there is a bit cleaner approach than an if block.

public String foo(@Nullable String mayBeNothing) {
   return Optional.ofNullable(mayBeNothing).orElse("Really Nothing");
}

You can also throw some exception in there by swapping .orElse to orElseThrow(() -> new Exception("Dont' send a null")).

If you don't want to use @Nullable, which adds nothing functionally, why not just name the parameter with mayBe... so your intention is clear.

Manabu Tokunaga
  • 956
  • 10
  • 19
  • 10
    Using Optional as a replacement of `if` statement this is an anti-pattern, because it unnecessarily creates a new object. Creator of Optional mentions this here: https://www.youtube.com/embed/Ej0sss6cq14?start=1664&end=1800 – Pavel Feb 16 '21 at 07:14