is and as Operators
Type checks and casts.
Checking and Casting Types
Kotlin offers two operators for working with types at runtime:
ischecks whether a value is of a typeascasts a value to a type
Both have safe variants you will learn here.
The is Operator
is returns a boolean telling you if a value matches a type.
It is the foundation for smart casts.
fun main() {
val x: Any = "hello"
println(x is String)
println(x is Int)
}All lessons in this course
- when as Expression
- Smart Casts
- is and as Operators
- Exhaustive when