is and as Operators
Type checks and casts.
is and as Operators is a free Kotlin Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.
Checking and Casting Types
Kotlin offers two operators for working with types at runtime:
ischecks whether a value is of a typeascasts 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/!ischeck typesascasts but can throwas?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:
isand!isfor type checksasfor casts that may throwas?for safe casts returning null
Next you will make when exhaustive to cover all cases.
Learn Kotlin with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “is and as Operators” lesson free?
Yes — any 3 lessons of the Kotlin Academy learning path, including “is and as Operators”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
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
- when as Expression
- Smart Casts
- is and as Operators
- Exhaustive when