From Java to Kotlin – Part VIII: The Ternary operator vs the Elvis Operator
Considering a move to Kotlin? Coming from a Java background? In this short series of blog posts, I’ll take a look at familiar, straightforward Java concepts and demonstrate how you can approach them in Kotlin. While many of these points have already been discussed in earlier posts by colleagues, my focus is simple: how you used to do it in Java, and how you do it in Kotlin.
Case 8: You want a sensible default.
If something is missing. Or null. Or both.
The problem
You have a nullable value. You want to use it if it exists. Otherwise, fall back to something reasonable.
This is not advanced logic. It’s everyday defensive programming.
Yet in Java, it rarely looks elegant.
The Java way
Java has the ternary operator:
String name = user.getName();
int length = name != null ? name.length() : -1;
System.out.println("Name length: " + length);
-
This works.
-
It’s explicit.
-
It’s also… noisy.
Things get worse when method calls are involved:
int length = user.getName() != null
? user.getName().length()
: -1;
Or when you start caching intermediate values just to stay readable.
Java does not have a null-aware operator. So you end up spelling out the null check every time.
The Kotlin way
Kotlin combines safe calls with the Elvis operator (?:):
val length = name?.length ?: "Unknown"
println("Name length: $length")
Read out loud, this says:
“Give me the length if name is not null — otherwise, use "Unknown".”
-
No temporary variables.
-
No repeated method calls.
-
No ceremony.
Just intent.
Why is it called the Elvis operator?
Because ?: looks like Elvis Presley.
Really.
It’s the hair… So now you will never unsee this again.
One of my colleagues (hi Jasper 👋) makes sure this is pointed out every single time.
Kotlin does not have a ternary operator
And it doesn’t need one.
In Kotlin:
-
ifis an expression, not a statement (*1) -
whenis an expression (*1) -
?:handles null-coalescing
So instead of:
int x = condition ? a : b;
You write:
val x = if (condition) a else b
The language stays smaller. The intent stays clearer.
Why this matters
Null-handling logic is everywhere. The less visual noise it creates, the easier it is to reason about your code.
The Elvis operator doesn’t add power. It removes friction.
Takeaway
Java makes you spell out your intent. Kotlin lets you express it.
And yes — it really does look like Elvis.
Final note
(*1) As my collegue Riccardo pointed out:
if and when are both expressions ànd statements in Kotlin.
There is actually also a slight difference between the two:
val isEven = Random.nextInt() % 2 == 0
// ✅ if as statement, no else branch, compiles fine
if (isEven) {
println("Even!")
}
// 🍕🍍if as expression, no else branch, does NOT compile
val isOdd = if (isEven) {
false
}
// ✅ when as statement, not all branches covered, compiles fine
when {
isEven -> println("Even!")
}
// 🍕🍍when as expression, not all branches covered, does NOT compile
val isOdd2 = when {
isEven -> false
}