Sobes.tech
Junior — Middle

What methods do you use to check if a variable contains a null value?

sobes.tech AI

Answer from AI

In Android (Java/Kotlin), there are several ways to check if a variable contains a null value:

  • In Java, simply compare with null:
if (variable == null) {
    // variable is null
}
  • In Kotlin, there is the safe call operator ?. and the Elvis operator ?::
if (variable == null) {
    // variable is null
}

// or
val length = variable?.length ?: 0 // if variable is null, length will be 0
  • Also in Kotlin, you can use the isNullOrEmpty() function for strings or collections.

Thus, the main method is to compare with null, and in Kotlin, there are additional convenient operators for safe handling of nullable types.