🐦 Kotlin Beginner

What is the is operator in Kotlin?

Answer

The is operator checks if an object is an instance of a given type at runtime, like Java's instanceof. But Kotlin adds smart casting — after a successful is check, the compiler automatically casts the variable to the checked type within the conditional scope, without requiring an explicit cast. Example: if (obj is String) { println(obj.length) } — inside the if block, obj is automatically treated as a String. The !is operator checks that an object is NOT of the given type.