Kotlin Programming
Conditional Operators
Kotlin Ternary Operator
Coding Skills
Programming Concepts

Kotlin Ternary Conditional Operator

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Kotlin does not have the classic condition ? a : b ternary operator. That is intentional, not an omission, because Kotlin uses if as an expression, which covers the same use case without introducing a second conditional syntax just for inline value selection.

Use if as an Expression

In Java or JavaScript, a ternary operator often appears in assignments. In Kotlin, the equivalent form is an if expression.

kotlin
1val a = 10
2val b = 20
3
4val max = if (a > b) a else b
5println(max)

This is the direct Kotlin replacement for a ternary expression. The result of the if is the value of the chosen branch, so it can be assigned, returned, or passed as an argument.

if Is More General Than a Ternary Operator

One reason Kotlin does not need a dedicated ternary operator is that if already scales from tiny to moderately complex conditions without changing constructs.

kotlin
1val score = 85
2
3val label = if (score >= 90) {
4    "excellent"
5} else if (score >= 70) {
6    "pass"
7} else {
8    "retry"
9}
10
11println(label)

In languages with a ternary operator, nested ternaries often become unreadable quickly. Kotlin keeps the same expression form whether the branches are one line or several.

Return Values from Functions the Same Way

Because if is an expression, it works naturally in return statements too.

kotlin
1fun accessLabel(isAdmin: Boolean): String {
2    return if (isAdmin) "full access" else "limited access"
3}
4
5println(accessLabel(true))

This is concise and idiomatic Kotlin. There is no need to search for a separate operator.

Use when for Many Branches

If the logic starts looking like a chain of nested ternaries, when is usually a better tool than forcing everything into if.

kotlin
1val statusCode = 404
2
3val message = when (statusCode) {
4    200 -> "ok"
5    404 -> "not found"
6    500 -> "server error"
7    else -> "unknown"
8}
9
10println(message)

when is clearer for multiple discrete cases and is one of the reasons Kotlin code rarely misses the traditional ternary syntax.

The Elvis Operator Is Not a Ternary Operator

The Elvis operator ?: is easy to confuse with a ternary operator because it contains a question mark and a colon, but it does something different. It is specifically for null fallback.

kotlin
1val nickname: String? = null
2val displayName = nickname ?: "anonymous"
3
4println(displayName)

This means "use nickname if it is not null, otherwise use anonymous". It does not evaluate an arbitrary boolean condition the way a ternary operator would.

Keep the Kotlin Version Idiomatic

When translating from Java, do not try to force a one-line ternary shape everywhere. Kotlin style favors clear expressions over preserving source-language punctuation.

kotlin
1val isLoggedIn = true
2val buttonText = if (isLoggedIn) "Sign out" else "Sign in"
3
4println(buttonText)

This is already short enough. Trying to mimic Java's operator-heavy style usually makes Kotlin code look translated rather than natural.

Common Pitfalls

  • Searching for a hidden ternary operator instead of using if as an expression.
  • Confusing the Elvis operator ?: with a general conditional operator.
  • Writing dense nested if expressions when when would be clearer.
  • Forgetting that both branches of an if expression should produce compatible types.
  • Translating Java code mechanically instead of rewriting it in idiomatic Kotlin.

Summary

  • Kotlin does not have a condition ? a : b ternary operator.
  • Use if (condition) a else b because if is an expression.
  • Use when when the branching logic has several cases.
  • Use the Elvis operator ?: only for null fallback, not as a ternary replacement.
  • Write Kotlin in Kotlin style rather than preserving Java syntax habits.

Course illustration
Course illustration

All Rights Reserved.