0Pricing
Kotlin Academy · Lesson

is and as Operators

Type checks and casts.

is and as Operators is a free Kotlin Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Checking and Casting Types

Kotlin offers two operators for working with types at runtime:

  • is checks whether a value is of a type
  • as casts a value to a type

Both have safe variants you will learn here.

The is Operator

is returns a boolean telling you if a value matches a type.

It is the foundation for smart casts.

fun main() {
    val x: Any = "hello"
    println(x is String)
    println(x is Int)
}

Negated Check with !is

Use !is to test that a value is NOT a given type.

This reads naturally in guard clauses.

fun main() {
    val x: Any = 42
    if (x !is String) {
        println("not a string")
    }
}

The as Operator

as performs an explicit cast. If the value really is that type, you get it back typed correctly.

Use it when the compiler cannot infer the type itself.

fun main() {
    val x: Any = "hello"
    val s = x as String
    println(s.length)
}

When as Fails

If the value is not the target type, as throws a ClassCastException.

This is the unsafe variant, so use it only when you are sure.

fun main() {
    val x: Any = 42
    try {
        val s = x as String
        println(s)
    } catch (e: ClassCastException) {
        println("Cannot cast Int to String")
    }
}

The Safe Cast as?

as? is the safe cast. It returns the value cast to the type, or null if the cast is impossible.

No exception is thrown.

fun main() {
    val x: Any = 42
    val s = x as? String
    println(s)
}

Safe Cast with Elvis

Pair as? with Elvis to provide a fallback when the cast fails.

This is a clean, crash-free pattern.

fun main() {
    val x: Any = 42
    val s = (x as? String) ?: "default"
    println(s)
}

is Drives Smart Casts

After an is check you usually do not need as at all, because smart casts kick in.

Reach for explicit as only when smart casting cannot apply.

fun main() {
    val x: Any = "smart"
    if (x is String) {
        println(x.uppercase()) // smart cast, no as needed
    }
}

Casting in Collections

Safe casts are useful when filtering mixed collections.

filterIsInstance uses is internally to keep only matching elements.

fun main() {
    val items: List<Any> = listOf(1, "two", 3, "four")
    val strings = items.filterIsInstance<String>()
    println(strings)
}

Choosing the Right Operator

A simple guide:

  • Need a boolean? use is
  • Sure of the type? use as
  • Unsure? use as? with a fallback
fun describe(x: Any): String {
    val text = x as? String ?: return "not text"
    return "text of length " + text.length
}

fun main() {
    println(describe("hello"))
    println(describe(99))
}

Putting It Together

The type operators give you full control:

  • is / !is check types
  • as casts but can throw
  • as? casts safely, returning null on failure
fun main() {
    val values: List<Any> = listOf(1, "hi", 2.5)
    for (v in values) {
        when {
            v is Int -> println("int " + v)
            v is String -> println("string " + v)
            else -> println("other")
        }
    }
}

Quick Check

Test your understanding of is and as.

Recap

You learned the type operators:

  • is and !is for type checks
  • as for casts that may throw
  • as? for safe casts returning null

Next you will make when exhaustive to cover all cases.

Frequently asked questions

Is the “is and as Operators” lesson free?

Yes — the full text of “is and as Operators” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.

What will I learn in “is and as Operators”?

Type checks and casts. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Academy?

No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “is and as Operators” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Academy lesson?

Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. when as Expression
  2. Smart Casts
  3. is and as Operators
  4. Exhaustive when
← Back to Kotlin Academy