Kotlin provides null safety in application. Null safety works on variables accepting null or not.NullpointerException is thrown the particular exception while sometimes application failure or system crashes. If anybody has been programming in Java or another, that concept is null. Kotlin compiler also has a null pointer exception if it finds any null reference in the code.
Nullable and Non-nullable types in kotlin-
- Kotlin type system of two types of reference non-null references and nullable references.
- A variable of type string can not hold null values. if we try to assign null then give a compile time error.
Kotlin program of non-nullable type
Output
Above example m1 variable if assigned null then gets an error while the compiler is.
Kotlin program of nullable type
Output
Elvis operator(?:)
The Elvis operator is used to return a null-value default value when the original variable is null.
Output
Happy Coding!