Skip to content

Instantly share code, notes, and snippets.

@kalaiselvan369
Last active June 12, 2023 11:24
Show Gist options
  • Save kalaiselvan369/4fc3e728dfbd657c5a54d5a37c509578 to your computer and use it in GitHub Desktop.
Save kalaiselvan369/4fc3e728dfbd657c5a54d5a37c509578 to your computer and use it in GitHub Desktop.
Dealing null values
fun main() {
val a: Int? = null
println(a)
var b: String? = "Hi"
// !! means non null assertion
for (char in b!!) {
if (char == 'i') {
b = null
}
}
println(b?.length) // Safe Call - prevent from throwing exception
val c = b?.length ?: 0 // usage of elvis
println(c)
val d: String = b!! // non-null assertion
print(d.length)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment