Extensions on Nullable Types and lateinit Property

Learn Kotlin’s null handling techniques through smart casting and lateinit properties.

Nullable types and extensions

Regular functions cannot be called on nullable variables. However, there is a special kind of function that can be defined such that it can be called on nullable variables. Kotlin stdlib defines the following functions that can be called on String?:

  • orEmpty: It returns the value if it is not null. Otherwise, it returns an empty string.

  • isNullOrEmpty: It returns true if the value is null or empty. Otherwise, it returns false.

  • isNullOrBlank: It returns true if the value is null or blank. Otherwise, it returns false.

Get hands-on with 1400+ tech skills courses.