Kotlin has no ternary operator (a ? b : c). How do you write the equivalent, and what does that tell you about if in Kotlin?
answer
- No ternary: use if/else as an expression
- val m = if (a > b) a else b
- else mandatory when the result is used
- Result type = common supertype of branches
- ?: is Elvis (null), not a ternary
basics
~20 sKotlin has no a ? b : c. You use if/else directly, because if returns a value. Write val m = if (a > b) a else b. The if/else expression takes the place of the ternary.
solid answer
~40 sKotlin deliberately omits the C-style ternary operator `a ? b : c` because its `if`/`else` is an expression, not just a statement. An expression evaluates to a value, so you can assign its result: `val max = if (a > b) a else b`. This single construct covers both branching control flow and value selection, so a separate ternary would be redundant. Because you are using the result, the `else` branch becomes mandatory — without it the expression could have no value to return. The result type is the least common supertype of both branches. This keeps the language smaller and the syntax uniform: there is one way to branch, whether or not you keep the result.
code
kotlin · 7 linesval a = 3
val b = 7
val max = if (a > b) a else b // replaces a > b ? a : b
println(max) // 7
val label = if (max > 5) "big" else "small"
println(label) // biggo deeper
Knows there is no ternary and writes val m = if (a > b) a else b correctly.
Explains statement-vs-expression and why else is mandatory only when the result is used.
Discusses result-type inference (least common supertype) and distinguishes ?: Elvis from a ternary.
Frames the omission as a deliberate language-design choice favoring one general expression construct over overlapping operators.
## The missing ternary Many languages (C, Java, JavaScript) have a **ternary conditional operator** written `condition ? valueIfTrue : valueIfFalse`. Kotlin has no such operator. Instead, Kotlin makes `if`/`else` an **expression**. ### Statement vs expression - A **statement** performs an action but yields no usable value (e.g. Java's `if`). - An **expression** evaluates to a value you can assign, return, or pass as an argument. In Kotlin, `if`/`else` is an expression, so the result can be assigned: ```kotlin val max = if (a > b) a else b ``` This is exactly the Java ternary `int max = a > b ? a : b;` rewritten with `if`. ### Why no ternary is needed Because `if` already returns a value, a separate ternary would duplicate functionality. Kotlin favors one general construct over two overlapping ones. ### The else is mandatory when used as an expression If you use the result, you must provide `else`, otherwise some path would yield no value: ```kotlin // Compiles: result used, else present val label = if (n > 0) "pos" else "non-pos" // Does NOT compile as an expression: no else // val label = if (n > 0) "pos" // error ``` When used purely as a statement (you ignore the result), `else` is optional: ```kotlin if (n > 0) println("positive") // fine, result discarded ``` ### Result type The type of the whole expression is the **least common supertype** of both branches. `if (c) 1 else 2` is `Int`; `if (c) "x" else null` is `String?`. ### Keywords/operators involved - `if`, `else` — the branching keywords. - `val` — captures the expression result immutably. - There is intentionally no `?:`-as-ternary; note `?:` in Kotlin is the **Elvis operator** (null-coalescing), a different thing.
- Is Kotlin's ?: a ternary operator?No. ?: is the Elvis operator: a ?: b yields a if non-null, otherwise b. It is binary null-coalescing, not a three-part conditional.
- What is the type of if (c) 1 else "x"?Any (or Comparable<*> & Serializable depending on inference) — the least common supertype of Int and String. Mixing unrelated types usually signals a design issue.
if/else is a vending machine: same lever to pick, and it always hands you something back.
saying these in an interview costs you the question
- Claiming Kotlin has a ? : ternary operator
- Confusing the Elvis operator ?: with a ternary
- Saying else is optional even when assigning the result
- Writing a verbose statement-style if with a temporary var instead of an expression
- Thinking if always requires else even when used as a statement