What is Kotlin's `when` and how does it differ from a Java/C-style `switch`?
answer
- No fall-through, no break
- else = default
- Statement or expression
- First matching branch wins, top to bottom
- Conditions can be expressions, not just constants
basics
~10 swhen is Kotlin's replacement for switch. You list conditions and pick the first matching branch. It can be used as a value-producing expression or as a plain statement, and branches don't fall through.
solid answer
~40 s`when` matches a subject against branches `condition -> result`, running the first match. Unlike C/Java `switch`, there is no fall-through and no `break`; each branch is independent. `else` is the catch-all (like `default`). It works as a statement (ignore the result) or an expression (the matched branch's value becomes the result, so it must yield a value and be exhaustive). Branch conditions aren't limited to constants: you can use any expression, comma-group several values (`1, 2 ->`), match ranges with `in`/`!in`, and test types with `is`/`!is`. A branch body can be a block `{ ... }` whose last expression is its value. `when` evaluates branch conditions top to bottom and stops at the first true one.
code
kotlin · 6 linesfun describe(x: Int): String = when (x) {
0 -> "zero"
1, 2, 3 -> "small"
in 4..9 -> "medium"
else -> "large"
}go deeper
Knows when replaces switch, uses else, and that there is no fall-through.
Explains statement vs expression use and that conditions can be ranges, types, or grouped values.
Articulates exhaustiveness for the expression form and that when compiles to efficient jump tables.
Discusses when to prefer when over polymorphism/sealed hierarchies for readability and how it interacts with exhaustiveness guarantees in API evolution.
## What `when` is `when` is Kotlin's multi-way branching construct, replacing the C/Java `switch`. The general form is: ```kotlin when (x) { 1 -> "one" 2, 3 -> "two or three" // comma-grouped branch in 4..10 -> "small" // range check else -> "other" // catch-all (like default) } ``` Each line is `condition -> body`. Kotlin evaluates the branch conditions **top to bottom** and executes the **first** one that matches, then stops. ## Key differences from `switch` - **No fall-through, no `break`.** In C/Java a `case` without `break` falls into the next one. Kotlin branches are isolated; you never write `break`. - **`else` instead of `default`** as the catch-all. - **Conditions are expressions, not just constants.** `switch` typically allows only compile-time constants. `when` branches can use ranges (`in 1..10`), type tests (`is String`), or — in the subjectless form — arbitrary boolean expressions. - **It can be an expression.** `val s = when (x) { ... }` assigns the matched branch's value. As a statement, you just run side effects and ignore the result. ## Statement vs expression - **Statement form:** result discarded; `else` is optional. - **Expression form:** the value of the chosen branch becomes the `when` value, so it must be **exhaustive** (every possibility covered, usually via `else`) and every branch must produce a value. ## Branch bodies A body can be a single expression or a block: ```kotlin val label = when (n) { 0 -> "zero" else -> { val sign = if (n > 0) "+" else "-" "$sign$n" // last expression of the block is the branch value } } ``` Under the hood `when` compiles to ordinary conditional jumps (or a `tableswitch`/`lookupswitch` for dense integer/enum subjects), so it's as efficient as a `switch`.
- Do you need `break` between branches?No. Kotlin `when` has no fall-through, so `break` is neither needed nor allowed as a fall-through control; each branch is independent.
- What value does a block-bodied branch produce?The value of the last expression in the block.
Like a receptionist scanning a checklist top to bottom and sending you to the first desk that fits — never to two desks at once.
saying these in an interview costs you the question
- Claiming `when` falls through like `switch` without `break`
- Thinking branch conditions must be compile-time constants
- Saying you must always write `break`
- Believing `when` can only be a statement, never an expression