kotlinkotlin-extension

How to check "instanceof " class in Kotlin?


In Kotlin class, I have method parameter as object (See Kotlin doc here ) for class type T. As object I am passing different classes when I am calling method. In Java we can able to compare class using instanceof of object which class it is.

So I want to check and compare at runtime which Class it is?

How can I check instanceof class in Kotlin?


Solution

  • Use is.

    if (myInstance is String) { ... }
    

    or the reverse !is

    if (myInstance !is String) { ... }