Kotlin - Type checking and Casting

Introduction

To check if a reference to an instance declared as some general type A is more specific type B, use Kotlin 'is' operator.

This is equivalent to the instanceof operator in Java:

fun isString(any: Any): Boolean { 
          return if (any is String) true else false 
} 

If the target type is invalid, then a ClassCastException will be thrown at runtime.

Related Topics