Skip to main content

Elvis Operator for Default Values in Kotlin

2 min read
Share:
On this page (12sections)

Introduction

Elvis Operator for Default Values is a fundamental concept every Kotlin developer should understand. Null safety is one of Kotlin’s signature features. It catches null-related bugs at compile time instead of crashing at runtime with a NullPointerException.

The Elvis operator ?: returns the left value when it is not null. In this tutorial you will learn the syntax, walk through a complete example program, study the sample output, and review best practices so you can apply the concept confidently in your own projects.

Definition

  • The Elvis operator ?: returns the left value when it is not null.
  • When the left value is null, the right-hand default value is returned.
  • It helps assign fallback values without long if-else checks.

Syntax

val length = text?.length ?: 0

Elvis Operator for Default Values in Kotlin Example Program in Kotlin

fun main(args: Array<String>) {
    fun displayLength(value: String?) {
        val length = value?.length ?: 0
        println("Length is $length")
    }
    displayLength("Little Drops")
    displayLength(null)
}

Sample Output

Length is 12
Length is 0

When to use

Use nullable types when a value may legitimately be absent — optional fields, parsed input, or database lookups that can miss.

How it works

  1. The program starts with a main function — the entry point that runs when you execute the file.

  2. val length = value?.length ?: 0 assigns or updates a value used later in the program.

  3. The println("Length is $length") statement writes a line to the console — this produces part of the sample output below.

  4. The Elvis operator ?: returns the left value when it is not null.

  5. Run the program in IntelliJ IDEA, Android Studio, or with the Kotlin command-line compiler (kotlinc / kotlin). Compare your console output with the sample output shown below.

Best Practices

  • Prefer non-nullable types by default; add ? only when null is a valid state.
  • Use safe call (?.) and Elvis (?:) instead of force unwrap (!!) unless you are certain.
  • Handle nullable collections and nested properties with ?. chains or let blocks.

Common Mistakes

  • Using !! everywhere instead of handling null safely — this reintroduces NPE risk.
  • Forgetting that platform types from Java are implicitly nullable.
  • Comparing nullable strings with == without considering both sides may be null.

Key Points

  • The Elvis operator ?: returns the left value when it is not null.
  • When the left value is null, the right-hand default value is returned.
  • It helps assign fallback values without long if-else checks.
  • Test the example locally and verify the output matches the sample.
  • Experiment by changing input values to see how behaviour changes.

Notes

  • Semicolons at the end of statements are optional in Kotlin.

Frequently Asked Questions

What is Elvis Operator for Default Values in Kotlin?
The Elvis operator ?: returns the left value when it is not null.
When should I use Elvis Operator for Default Values?
Use nullable types when a value may legitimately be absent — optional fields, parsed input, or database lookups that can miss.
How is Elvis Operator for Default Values different from Java?
It helps assign fallback values without long if-else checks.
How do I practice this topic?
Copy the example program into IntelliJ IDEA or Android Studio, run it, then modify values or add print statements to confirm your understanding.

Related Tutorials

Search tutorials