Usually, cast operator throws an exception if the cast is not possible. Thus, we call it unsafe. The unsafe cast in Kotlin is done by an infix operator as (see operator precedence):
Note that null cannot be cast to String as this type is not nullable, i.e. if y is null, the code above throws an exception.
In order to match Java cast semantics we have to have nullable type at cast right hand side, like
"Safe" (nullable) cast operator
To avoid an exception being thrown, one can use a "safe" cast operator as? that returns null on failure:
Note that despite the fact that the right-hand side of as? is a non-null type String the result of the cast is nullable.