javax.annotation: @Nullable vs @CheckForNull

JavaAnnotationsStatic AnalysisFindbugs

Java Problem Overview


What is the difference between the two? Both seem to mean that the value may be null and should be dealt with accordingly i.e. checked for null.

Update: The two annotations above are part of JSR-305/FindBugs: http://findbugs.sourceforge.net/manual/annotations.html

Java Solutions


Solution 1 - Java

I think it is pretty clear from the link you added: if you use @CheckForNull and the code that uses the value does not check for null, FindBugs will show it as an error.

FindBugs will ignore @Nullable.

> In practice this annotation is useful only for overriding an overarching NonNull annotation.

Use @CheckForNull in the cases when the value must always be checked. Use @Nullable where null might be OK.

EDIT: it seems that @CheckForNull is not well supported at the moment, so I suggest avoiding it and using @NonNull (also see https://stackoverflow.com/questions/4963300/which-notnull-java-annotation-should-i-use). Another idea would be to get in touch directly with the FindBugs developers, and ask their opinion about the inconsistency in the documentation.

Solution 2 - Java

@Nonnull and @Nullable are correctly handled by IntelliJ IDEA. FindBugs found the problem with @Nonnull but missed those for @Nullable and @CheckForNUll. Problems which were detected by IDEA and FindBugs are marked with comments.

package com.db.icestation;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class test {

    @Nullable public String nullable() {
        return "";
    }

    @Nonnull public String nonnull() {
        return null; // IDEA, findbugs
    }

    @CheckForNull public String checkForNull() {
        return null;
    }

    public static void main(String[] args) {
        System.out.println(new test().nullable().length()); // IDEA
        System.out.println(new test().nonnull().length());
        System.out.println(new test().checkForNull().length());
    }
}

Solution 3 - Java

In the IntelliJ Idea @javax.annotation.Nullable is supported by default and any attempts to dereference @Nullable arguments or return values will result in warning.

@alexander-pavlov, You could add @javax.annotation.CheckForNull in configuration of "Constant conditions & exceptions" inspection. Go File->Settings->Inspections->Probable bugs->Constant conditions & exceptions->Configure annotations.

I prefer doing this as @CheckForNull has more clear meaning than @Nullable as @lbalazscs mentioned in his answer above.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionvitalyView Question on Stackoverflow
Solution 1 - JavalbalazscsView Answer on Stackoverflow
Solution 2 - JavaAlexander PavlovView Answer on Stackoverflow
Solution 3 - JavaIlya SilvestrovView Answer on Stackoverflow