How to check "instanceof " class in kotlin?

You can use is:

class B
val a: A = A()
if (a is A) { /* do something */ }
when (a) {
  someValue -> { /* do something */ }
  is B -> { /* do something */ }
  else -> { /* do something */ }
}

Combining when and is:

when (x) {
    is Int -> print(x + 1)
    is String -> print(x.length + 1)
    is IntArray -> print(x.sum())
}

copied from official documentation


We can check whether an object conforms to a given type at runtime by using the is operator or its negated form !is.

Example:

if (obj is String) {
    print(obj.length)
}

if (obj !is String) {
    print("Not a String")
}

Another Example in case of Custom Object:

Let, I have an obj of type CustomObject.

if (obj is CustomObject) {
    print("obj is of type CustomObject")
}

if (obj !is CustomObject) {
    print("obj is not of type CustomObject")
}

Use is.

if (myInstance is String) { ... }

or the reverse !is

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