Last active
June 12, 2023 11:24
-
-
Save kalaiselvan369/4fc3e728dfbd657c5a54d5a37c509578 to your computer and use it in GitHub Desktop.
Dealing null values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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