Kotlin Ternary Conditional Operator
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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.
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.
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.
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.
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.
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.
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
ifas an expression. - Confusing the Elvis operator
?:with a general conditional operator. - Writing dense nested
ifexpressions whenwhenwould be clearer. - Forgetting that both branches of an
ifexpression should produce compatible types. - Translating Java code mechanically instead of rewriting it in idiomatic Kotlin.
Summary
- Kotlin does not have a
condition ? a : bternary operator. - Use
if (condition) a else bbecauseifis an expression. - Use
whenwhen 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.
Related reading
- Label Alignment in iOS 6 - UITextAlignment deprecated
- Label under image in UIButton
- latchused for awaiting async response freezes the WebView and the UI
- Launch an app from within another iPhone
- Launch custom android application from android browser
- Launch iOS simulator from Xcode and getting a black screen, followed by Xcode hanging and unable to stop tasks
- Launch Screen storyboard not displaying image
- Launching Google Maps Directions via an intent on Android
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.