> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Kotlin Elvis

The `?:` operator, commonly referred to as the Elvis operator, is a binary null-coalescing operator in Kotlin. It evaluates its left-hand operand and returns it if it resolves to a non-null value; if the left-hand operand evaluates to `null`, it evaluates and returns the right-hand operand.

## Syntax

```kotlin theme={"dark"}
val leftExpression: String? = "Kotlin"
val rightExpression: String = "Default"

val result = leftExpression ?: rightExpression
```

## Evaluation Mechanics

**1. Short-Circuit Evaluation**
The `?:` operator evaluates lazily. The right-hand operand is strictly evaluated if and only if the left-hand operand resolves to `null`. If the left side is non-null, the right side is completely ignored, preventing potential side effects or unnecessary computation.

```kotlin theme={"dark"}
fun rightSideFunction(): String = "Fallback"
val nullableValue: String? = "Primary"

// rightSideFunction() is never called because nullableValue is not null
val result = nullableValue ?: rightSideFunction()
```

**2. Type Resolution**
The compiler determines the return type of the `?:` expression based on the least upper bound (lowest common supertype) of the **non-nullable type of the left operand** and the **type of the right operand**.

* If the left operand is of type `T?` and the right operand is of type `T`, the resulting expression is guaranteed to be of the non-nullable type `T`.
* If the left operand is of type `T?` but the right operand is of a nullable type `U?`, the resulting expression evaluates to a nullable type.

```kotlin theme={"dark"}
val a: String? = "Kotlin"
val b: String = "Java"
val c: String? = null

// Least upper bound of String (non-null left) and String (right)
val result1: String = a ?: b  

// Least upper bound of String (non-null left) and String? (right)
val result2: String? = a ?: c 
```

**3. Operator Precedence**
The `?:` operator has lower precedence than infix function calls and arithmetic operators (both additive and multiplicative), but higher precedence than comparison (relational) and equality operators. Consequently, expressions on the right-hand side involving arithmetic operators are evaluated together before the `?:` operator is applied.

```kotlin theme={"dark"}
val a: Int? = null
val b: Int = 2
val c: Int = 3

// Evaluates as `a ?: (b + c)`, resulting in 5.
// It does NOT evaluate as `(a ?: b) + c`.
val result = a ?: b + c
```

**4. Integration with the `Nothing` Type**
In Kotlin, control flow statements such as `return` and `throw` are expressions that evaluate to the `Nothing` type. Because `Nothing` is a subtype of all other types in Kotlin's type system, these expressions are syntactically valid as the right-hand operand of the `?:` operator.

If the left operand is `null`, the `Nothing` expression is evaluated, immediately terminating the current control flow path. The compiler understands that if execution continues past this statement, the left operand could not have been `null`. Consequently, if the left operand is a reference eligible for smart casting (such as a local `val` or a function parameter), the compiler automatically smart-casts it to its non-nullable counterpart for the remainder of the scope. The resulting value of the `?:` expression itself is inferred as a non-nullable type.

```kotlin theme={"dark"}
fun processData(nullableData: String?) {
    // If nullableData is null, the function returns and execution halts.
    // The assigned variable 'data' is inferred as a non-nullable String.
    val data = nullableData ?: return
    
    // Because execution continued and 'nullableData' is an eligible reference 
    // (a local parameter), it is smart-cast to a non-nullable type.
    val length = nullableData.length
}

fun processValue(nullableValue: String?) {
    // Similarly, throwing an exception yields Nothing
    val value = nullableValue ?: throw IllegalArgumentException("Null encountered")
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Kotlin Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
