Search code examples
javaintellij-ideaannotationsnullablenotnull

Does @NotNull work at runtime


I have recently decided to start using null checksum annotations (@NotNull), I use Intellij Idea's annotations library to get access these. I am trying to figure out whether or not these annotations will work at runtime to check if values are null and if so how does it handle these errors (e.g. does it throw a NullPointerException, does it just return a default value (e.g. null, 0, false)). Also if these annotations do not work is there a different set that may be more standardized annotations that will work at runtime (e.g. javax)? If these do not work should I stop using them and return to a standard null checksum (if(x == null)) or should use both the annotations as well as standard null checksums? And while I am here are annotations like @Nullable a good idea?


Solution

  • Yes, it checks at runtime and throws an IllegalArgumentException when null.

    You can disable/enable this in the settings Preferences | Build, Execution, Deployment | Compiler. Add runtime assertions for notnull-annotated methods and parameters checkbox, second from the top