Type Checking and Smart Casts
Swipe to show menu
The is Operator in Kotlin
Kotlin uses the is operator to check if an object is of a specific type at runtime. This operator serves the same purpose as Java's instanceof operator but is more concise and fits naturally with Kotlin's syntax.
When you write if (obj is String), Kotlin checks whether obj is an instance of the String class or a subclass. If the check passes, you can safely use obj as a String inside that block, thanks to Kotlin's smart cast feature.
Comparison with Java:
- In Java, you write:
if (obj instanceof String); - In Kotlin, you write:
if (obj is String).
Practical notes:
- The
isoperator automatically handles null checks; ifobjisnull, the result isfalse; - You do not need to cast the object manually after an
ischeckโKotlin smart casts it for you in the safe scope; - To check if an object is not of a type, use
!is(e.g.,if (obj !is String)).
Use the is operator for safe, readable type checks in Kotlin, and rely on smart casts to reduce boilerplate and potential casting errors.
Main.kt
12345678910111213141516171819202122232425262728293031323334353637package com.example // Define a base class open class Animal(val name: String) // Define two subclasses class Dog(name: String) : Animal(name) { fun bark() = println("$name says: Woof!") } class Cat(name: String) : Animal(name) { fun meow() = println("$name says: Meow!") } fun makeSound(animal: Animal) { // Use the 'is' operator to check the actual type of 'animal' if (animal is Dog) { // Inside this block, 'animal' is smart-cast to Dog animal.bark() } else if (animal is Cat) { // Here, 'animal' is smart-cast to Cat animal.meow() } else { println("${animal.name} makes an unknown sound.") } } fun main() { val dog = Dog("Buddy") val cat = Cat("Whiskers") val unknown = Animal("Mystery") // Demonstrate type checking and smart casts makeSound(dog) // Should call Dog.bark() makeSound(cat) // Should call Cat.meow() makeSound(unknown) // Should print unknown sound }
Smart Casts
Kotlin's smart casts feature automatically casts a variable to a more specific type after a successful type check using the is operator. This reduces the need for explicit type casts that are often required in Java, making your code cleaner and safer.
When you check if a variable is of a certain type using is, Kotlin automatically treats the variable as that type within the scope of the check. You do not need to use an explicit cast like (Type) in Java. This eliminates repetitive casting and reduces the risk of ClassCastException at runtime.
Practical benefits:
- Eliminates most explicit casts after type checks;
- Improves code readability and maintainability;
- Reduces the chance of runtime casting errors.
Example:
fun printStringLength(obj: Any) {
if (obj is String) {
// No explicit cast needed; obj is treated as String here
println(obj.length)
}
}
In Java, you would need to explicitly cast the object:
void printStringLength(Object obj) {
if (obj instanceof String) {
String s = (String) obj;
System.out.println(s.length());
}
}
With smart casts, Kotlin makes type-safe programming more concise and less error-prone.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat