Kotlin - Null Safety
How can you safely cast a nullable Any? variable to String? and provide a default value if the cast fails?
val obj: Any? = getObject() val str: String = (obj ___) ?: "default"
How can you safely cast a nullable Any? variable to String? and provide a default value if the cast fails?
val obj: Any? = getObject() val str: String = (obj ___) ?: "default"
as? returns a nullable String or null if cast fails.?: provides "default" if the cast returns null.15+ quiz questions · All difficulty levels · Free
Free Signup - Practice All Questions