Home > DeveloperSection > Forums > Which @NotNull Java annotation should I use?
Mikki Halpin
Mikki Halpin

Total Post:23

Points:161
Posted on    July-17-2015 11:08 PM

 Java Java 
Ratings:


 1 Reply(s)
 582  View(s)
Rate this:
I'm looking to make my code more readable as well as use tooling like IDE code inspection and/or static code analysis (FindBugs and Sonar) to avoid NullPointerExceptions. Many of the tools seem incompatible with each others' @NotNull/@NonNull/@Nonnull annotation and listing all of them in my code would be terrible to read. Any suggestions of which one is the 'best'? Here is the list of equivalent annotations I've found:

javax.validation.constraints.NotNull
Created for runtime validation, not static analysis.
documentation

edu.umd.cs.findbugs.annotations.NonNull
Used by Findbugs static analysis and therefore Sonar
documentation

javax.annotation.Nonnull
This might work with Findbugs too, but JSR-305 is inactive.
source

com.intellij.annotations.NotNull
Used by IntelliJ IDEA IDE for static analysis.
documentation

lombok.NonNull
Used to control code generation in Project Lombok.


Mayank Tripathi
Mayank Tripathi

Total Post:397

Points:3117
Posted on    July-18-2015 8:18 AM

For Android projects you should use android.support.annotation.NonNull and android.support.annotation.Nullable. These and other helpful Android-specific annotations are available in the Support Library.

Don't want to miss updates? Please click the below button!

Follow MindStick