Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of @Nullable java (android)

I saw this video on how to create fragments in Android and I'm unable to understand the meaning of @Nullable here. Also in the video (thenewboston) @Nullable was used on parameter:

@Override
public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState)
 {
return super.onCreateView(inflater, container, savedInstanceState);
    }

But later on when I tried to create the same override method Android Studio generated this:

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

}

Can someone please explain me the use of @Nullable here?

like image 649
Shreyans Avatar asked Sep 25 '15 13:09

Shreyans


People also ask

What does @nullable mean in Java?

@NullableMethod calls that can return null. Variables (fields, local variables, and parameters), that can be null.

What is @nullable in Android Studio?

Denotes that a parameter, field or method return value can be null. When decorating a method call parameter, this denotes that the parameter can legitimately be null and the method will gracefully deal with it. Typically used on optional parameters.

What is @nullable in spring?

Annotation Type NullableA common Spring annotation to declare that annotated elements can be null under some circumstance. Leverages JSR-305 meta-annotations to indicate nullability in Java to common tools with JSR-305 support and used by Kotlin to infer nullability of Spring API.

What is the use of @NonNull in Java?

@NonNull – The compiler can determine cases where a code path might receive a null value, without ever having to debug a NullPointerException. @ReadOnly – The compiler will flag any attempt to change the object. This is similar to Collections.


1 Answers

It means that the return value of onCreateView method can be null.

From the Android documentation:

Denotes that a parameter, field or method return value can be null.

When decorating a method call parameter, this denotes that the parameter can legitimately be null and the method will gracefully deal with it. Typically used on optional parameters.

When decorating a method, this denotes the method might legitimately return null.

This is a marker annotation and it has no specific attributes.

You can find more details and examples in this Android Support Annotation page

like image 128
abarisone Avatar answered Nov 05 '22 13:11

abarisone