Avatar ·

How to check object type (instanceof) in Kotlin?

Hello everyone! I just started learning Kotlin after Java and ran into a problem. In Java, I'm used to using the `instanceof` operator to check the type of an object, for example: ```java if (obj instanceof String) { String str = (String) obj; System.out.println(str.length()); } ``` But in Kotlin I can't find an equivalent. I tried to write something like `obj instanceof String`, but the compiler gives an error. I know that Kotlin has the `is` keyword, but I'm not sure how to use it correctly. Plus, in Java after the `instanceof` check I had to explicitly cast the type, but in Kotlin, as far as I understand, this is done automatically? Or not? Please tell me how to correctly check the type of an object in Kotlin and safely cast it to the desired type? I'm interested not only in the basic