Junior — Middle
Milliseid meetodeid kasutad, et kontrollida, kas muutuja väärtus on null?
sobes.tech AI
Vastus AI-lt
In Android (Java/Kotlin), to check if a variable contains a null value, there are several methods:
- 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 function
isNullOrEmpty()for strings or collections.
Thus, the main method is comparison with null, and in Kotlin, there are convenient operators for safe handling of nullable types.