gstackoverflow
gstackoverflow

Reputation: 37034

@Nonnull annotation is only marker?

I'm new to @Nonnull annotation.

I tried to use it and write the following test:

public class TestNonull{
    public static void doSmth(@Nonnull Integer integer){}
    public static void main(final String[] arguments){
       doSmth(null);
       doSmth(1);
    }
}

I don't see anything in console.

Is it normal?

Upvotes: 1

Views: 229

Answers (2)

Blank Chisui
Blank Chisui

Reputation: 1073

@Nonnull and @Nullable are just markers for your IDE or code analyzers. The javac compiler does not insert any special code for methods annotaded this way.

Upvotes: 6

Jordi Reina
Jordi Reina

Reputation: 111

This is only an annotation for your IDE. You should check if the parameter is null before calling the function.

Like that:

String capitalize(@NonNull String in) {
        return in.toUpperCase();                // no null check required
    }
    void caller(String s) {
        if (s != null)
            System.out.println(capitalize(s));  // preceding null check is required
    }

Check -> Using null annotations

Upvotes: 0

Related Questions