We will demonstrate some type checks:
if (a is String) // If a is instance of String
if (a !is String) // If a is not instance of String
And example for casting:
fun castCheck(a: Any) {
if (a is String) {
println(a) // a is automatically cast to String!
}
}
We don't need to add “(String) a ...” as we would do in Java! This work for both when-expressions and while-loops! Smart casts do not work when the compiler can't guarantee that the variable cannot change between the check and the usage.