Using this article, You can learn how to check data type of variable or class of a particular instance at runtime. You can also learn an important feature smart casts in Kotlin.
In Java, instanceof keyword is used to check of data type. In Kotlin, We have is and !is keywords to check data type of variables.
In smart casting, Compiler automatically converts the variable to a particular class reference after it is matched with conditional operator. In many cases, we do not need to use explicit casting in kotlin. We can use smart casts instead of explicit casts in this cases.
Unsafe cast operator: as
If the cast is not possible then cast operator throws an exception. We can call it unsafe cast. For unsafe cast kotlin provides as operator.
Safe cast operator: as?
If you want to avoid an exception while casting then you need to use safe casting. For this kotlin provides safe cast operator as?.