Sling Academy
Home/Kotlin/What is the Elvis Operator (`?:`) in Kotlin?

What is the Elvis Operator (`?:`) in Kotlin?

Last updated: November 30, 2024

The Elvis operator ?: in Kotlin is a concise operator that is primarily used for handling null values in a more readable way. This operator is particularly beneficial in Kotlin, where nullability is a key concept.

Understanding Null Safety in Kotlin

Before we delve into the Elvis operator, it’s important to understand Kotlin’s approach to null safety. In Kotlin, types are non-null by default. This means if you have a variable of a certain type, it cannot be assigned null unless explicitly stated. You can declare a variable that can hold a null value by appending a question mark (e.g., String?), making it nullable.

val nullableString: String? = null

Basic Usage of the Elvis Operator

The Elvis operator is used to provide a default value when a nullable expression evaluates to null. It takes the form a ?: b and returns b if a is null, otherwise, it returns a.

var length: Int = nullableString?.length ?: 0

In the above code, nullableString?.length may yield null if nullableString is null, hence the Elvis operator ?: assigns 0 to length

Practical Examples

Example 1: Simple assignment with the Elvis operator

fun getNameLength(name: String?): Int {
    return name?.length ?: -1
}

val nameLength = getNameLength(null)  // Returns -1 because name is null

Example 2: Using Elvis Operator with Function Calls

fun maybeGetString(): String? {
    return null  // Simulating a situation where null might be returned
}

val result = maybeGetString() ?: "Default String"
println(result)  // Prints "Default String" because the function returned null

Example 3: Complex Objects

If you work with more complex objects, using the Elvis operator can save you from a lot of null checks:

data class Person(val name: String?, val age: Int?)

val person: Person? = Person(null, 25)

val displayName = person?.name ?: "Unknown"
println(displayName)  // Prints "Unknown" since person.name is null

Conclusion

The Elvis operator ?: provides a neat way to handle nulls in Kotlin. By reducing the verbosity associated with null checks, the operator allows developers to write more concise and readable code. Utilizing the Elvis operator is crucial in a language like Kotlin that takes null safety seriously.

Next Article: Using the Elvis Operator for Default Values in Kotlin

Previous Article: Chaining Safe Calls for Complex Operations in Kotlin

Series: Null Safety in Kotlin

Kotlin

You May Also Like

  • How to Use Modulo for Cyclic Arithmetic in Kotlin
  • Kotlin: Infinite Loop Detected in Code
  • Fixing Kotlin Error: Index Out of Bounds in List Access
  • Setting Up JDBC in a Kotlin Application
  • Creating a File Explorer App with Kotlin
  • How to Work with APIs in Kotlin
  • What is the `when` Expression in Kotlin?
  • Writing a Script to Rename Multiple Files Programmatically in Kotlin
  • Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin
  • Chaining Safe Calls for Complex Operations in Kotlin
  • Using the Elvis Operator for Default Values in Kotlin
  • Combining Safe Calls and the Elvis Operator in Kotlin
  • When to Avoid the Null Assertion Operator (`!!`) in Kotlin
  • How to Check for Null Values with `if` Statements in Kotlin
  • Using `let` with Nullable Variables for Scoped Operations in Kotlin
  • Kotlin: How to Handle Nulls in Function Parameters
  • Returning Nullable Values from Functions in Kotlin
  • Safely Accessing Properties of Nullable Objects in Kotlin
  • How to Use `is` for Nullable Type Checking in Kotlin