Introduction
The when expression in Kotlin replaces Java's switch statement, but it is far more powerful. Besides matching exact values, when can match ranges, check types with is, evaluate arbitrary boolean conditions, and be used as an expression that returns a value - similar to how if-else can return a value. This makes when one of the most versatile control-flow tools in the language.
Cricket analogy: A commentator's decision tree does more than match an exact score like a switch: it checks ranges ('50s in this over'), types of dismissal with is CaughtOut, and arbitrary conditions like required run rate, then returns a verdict as a value, much like Kotlin's when expression.
Syntax
// As a statement
when (x) {
1 -> println("One")
2, 3 -> println("Two or three")
in 4..10 -> println("Between 4 and 10")
is String -> println("It's a string")
else -> println("Something else")
}
// As an expression
val description = when (x) {
1 -> "One"
else -> "Not one"
}Explanation
Each branch of a when block starts with a condition followed by -> and the code (or value) to run when that condition matches. Branches can be comma-separated values, ranges (using in), type checks (using is, which also smart-casts the variable inside that branch), or even arbitrary boolean expressions when no subject is given after when. When when is used as an expression to produce a value, the compiler requires an else branch unless the subject type is exhaustive - for example a sealed class hierarchy or an enum class where every possible case is covered by the other branches.
Cricket analogy: Each when branch is like an umpire's ruling: comma-separated dismissals, an 'in' range for overs 16..20 (death overs), or 'is RunOut' which smart-casts the dismissal details; when used to return a verdict, an exhaustive sealed DismissalType needs no else branch.
Example
fun describe(obj: Any): String = when (obj) {
1 -> "One"
"hello" -> "Greeting"
in 2..10 -> "A number between 2 and 10"
is Long -> "Long number"
is String -> "A string: $obj"
else -> "Unknown"
}
fun main() {
println(describe(1))
println(describe("hello"))
println(describe(5))
println(describe(42L))
println(describe("other text"))
}
// Output:
// One
// Greeting
// A number between 2 and 10
// Long number
// A string: other textKey Takeaways
- when replaces Java's switch statement and is far more flexible.
- Branches can match exact values, comma-separated values, ranges, or types (
is). - when can be used without a subject, acting like a chain of if-else conditions.
- when used as an expression must be exhaustive - typically requiring an else branch.
- Type-check branches (
is String) automatically smart-cast the variable inside that branch.
Practice what you learned
1. What Java construct does Kotlin's when expression most closely replace?
2. Which of these is NOT something a when branch can match on?
3. When is the else branch required in a when expression used to produce a value?
4. What happens to a variable checked with `is String ->` inside that when branch?
5. How can when be used without a subject expression (i.e. `when { ... }`)?
Was this page helpful?
You May Also Like
if-else Expressions in Kotlin
Learn how Kotlin's if-else works as both a statement and a value-returning expression, replacing the ternary operator.
Sealed Classes in Kotlin
Sealed classes restrict a class hierarchy to a known, closed set of subclasses, enabling exhaustive when checks.
Enum Classes in Kotlin
Enum classes define a fixed set of singleton constants that can carry properties and methods.
Smart Casts in Kotlin
After a null-check or type-check, Kotlin's compiler automatically treats a variable as the narrowed type within that scope, no explicit cast needed.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics