Kotlin - Null Safety
Identify the error in this Kotlin code snippet and fix it:
val obj: Any = "Hello" val num: Int = obj as? Int println(num)
Identify the error in this Kotlin code snippet and fix it:
val obj: Any = "Hello" val num: Int = obj as? Int println(num)
obj as? Int returns null if cast fails, so the result is nullable Int?.num as non-nullable Int causes a type mismatch error. Changing it to Int? fixes this.15+ quiz questions · All difficulty levels · Free
Free Signup - Practice All Questions