Skip to main content

Smart Casts in Kotlin

2 min read
Share:
On this page (12sections)

Introduction

Smart Casts is a fundamental concept every Kotlin developer should understand. Null safety is one of Kotlin’s signature features. It catches null-related bugs at compile time instead of crashing at runtime with a NullPointerException.

After an is check, the compiler automatically casts the variable to the subtype. In this tutorial you will learn the syntax, walk through a complete example program, study the sample output, and review best practices so you can apply the concept confidently in your own projects.

Definition

  • After an is check, the compiler automatically casts the variable to the subtype.
  • Smart casts work when the compiler can prove the value cannot change between the check and use.
  • They remove the need for manual casting after type checks.

Syntax

if (value is String) {
    println(value.length)
}

Smart Casts in Kotlin Example Program in Kotlin

fun describe(value: Any): String {
    if (value is String) return "String of length ${value.length}"
    if (value is Int) return "Int value $value"
    return "Unknown type"
}

fun main() {
    println(describe("Kotlin"))
    println(describe(42))
}

Sample Output

String of length 6
Int value 42

When to use

Use nullable types when a value may legitimately be absent — optional fields, parsed input, or database lookups that can miss.

How it works

  1. The program starts with a main function — the entry point that runs when you execute the file.

  2. After an is check, the compiler automatically casts the variable to the subtype.

  3. Run the program in IntelliJ IDEA, Android Studio, or with the Kotlin command-line compiler (kotlinc / kotlin). Compare your console output with the sample output shown below.

Best Practices

  • Prefer non-nullable types by default; add ? only when null is a valid state.
  • Use safe call (?.) and Elvis (?:) instead of force unwrap (!!) unless you are certain.
  • Handle nullable collections and nested properties with ?. chains or let blocks.

Common Mistakes

  • Using !! everywhere instead of handling null safely — this reintroduces NPE risk.
  • Forgetting that platform types from Java are implicitly nullable.
  • Comparing nullable strings with == without considering both sides may be null.

Key Points

  • After an is check, the compiler automatically casts the variable to the subtype.
  • Smart casts work when the compiler can prove the value cannot change between the check and use.
  • They remove the need for manual casting after type checks.
  • Test the example locally and verify the output matches the sample.
  • Experiment by changing input values to see how behaviour changes.

Notes

  • Semicolons at the end of statements are optional in Kotlin.

Frequently Asked Questions

What is Smart Casts in Kotlin?
After an is check, the compiler automatically casts the variable to the subtype.
When should I use Smart Casts?
Use nullable types when a value may legitimately be absent — optional fields, parsed input, or database lookups that can miss.
How is Smart Casts different from Java?
They remove the need for manual casting after type checks.
How do I practice this topic?
Copy the example program into IntelliJ IDEA or Android Studio, run it, then modify values or add print statements to confirm your understanding.

Related Tutorials

Search tutorials