Kotlin Notes Help

Type checks and casts

is and !is operators

These operators are used to perform a runtime check that identifies whether an object conforms to a given type:

if (obj is String) { print(obj.length) } if (obj !is String) { // Same as !(obj is String) print("Not a String") } else { print(obj.length) }

Smart Casts

fun demo(x: Any) { if (x is String) { print(x.length) // x is automatically cast to String } }
if (x !is String) return print(x.length) // x is automatically cast to String
// x is automatically cast to String on the right-hand side of `||` if (x !is String || x.length == 0) return // x is automatically cast to String on the right-hand side of `&&` if (x is String && x.length > 0) { print(x.length) // x is automatically cast to String }

"Unsafe" cast operator

Usually, the cast operator throws an exception if the cast isn't possible. Thus, it's called unsafe.

val x: String = y as String

Note that null cannot be cast to String, as this type is not nullable.

If y is null, the code above throws an exception.

To make code like this correct for null values, use the nullable type on the right-hand side of the cast:

val x: String? = y as String?

"Safe" cast operator

To avoid exceptions, use the safe cast operator as?, which returns null on failure.

val x: String? = y as? String
Last modified: 19 February 2024