Nullability and Java
We know that Kotlin requires us to explicitly define references that can hold null values. Java on the other hand is much more lenient about nullability, so we may wonder how Kotlin handles types coming from Java (basically the whole Android SDK and libraries written in Java). Whenever possible, the Kotlin compiler will determine type nullability from the code and represent types as actual nullable or non-nullable types using nullability annotations.
Note
The Kotlin compiler supports several flavors of nullability annotations, including:
- Android (
com.android.annotations
andandroid.support.annotations
) - JetBrains (
@Nullable
and@NotNull
from theorg.jetbrains.annotations
package) - JSR-305 (
Javax.annotation
)
We can find the full list in the Kotlin compiler source code (https://github.com/JetBrains/kotlin/blob/master/core/descriptor.loader.Java/src/org/jetbrains/kotlin/load/Java/JvmAnnotationNames.kt).
We have seen this previously in Activity's onCreate
method, where the savedInstanceState...