How to check "instanceof " class in kotlin?

pRaNaY picture pRaNaY · May 21, 2017 · Viewed 78.2k times · Source

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?

Answer

nhaarman picture nhaarman · May 21, 2017

Use is.

if (myInstance is String) { ... }

or the reverse !is

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