Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
null safety with conditional if
// check for null in conditions
val trainingName: String? = "Learn Kotlin in 45 minutes"
if (trainingName != null && trainingName.isNotEmpty()) {
print("String of length ${trainingName.length}")
} else {
print("Empty string")
}
val platform: String? = null
val language = "Kotlin"
println(platform?.length) // safe call
println(language.length) // unnecessary safe call
val lengthOfWord = platform!!.length // !! operator
val numberOfLetters: Int? = lengthOfWord as? Int // safe cast
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment