> ## 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 Post-Increment

The `++` (increment) operator is a unary operator that increases the value of a mutable variable. In Kotlin, it is not a primitive-only operation but rather syntactic sugar for the `inc()` operator function combined with a reassignment. Because Kotlin treats the increment operation as a reassignment, the operand must be declared as a mutable variable (`var`), not a read-only reference (`val`).

## Evaluation Modes

The operator exhibits different evaluation semantics based on its position relative to the operand:

* **Prefix (`++a`):** The variable is incremented first, and the expression evaluates to the *new* (incremented) value.
* **Postfix (`a++`):** The expression evaluates to the *original* value of the variable, and the variable is incremented afterward.

```kotlin theme={"dark"}
var prefixVar = 10
val prefixResult = ++prefixVar 
// prefixVar is 11, prefixResult is 11

var postfixVar = 10
val postfixResult = postfixVar++ 
// postfixVar is 11, postfixResult is 10
```

## Compiler Translation

Under the hood, Kotlin translates the `++` operator into a call to the `inc()` function followed by an assignment back to the original variable.

**Prefix Translation:**

```kotlin theme={"dark"}
// Source code
val b = ++a

// Conceptual compiler translation
a = a.inc()
val b = a
```

**Postfix Translation:**
To preserve the original value for the expression while ensuring the getter of `a` is evaluated exactly once, the compiler uses a temporary reference.

```kotlin theme={"dark"}
// Source code
val b = a++

// Conceptual compiler translation
val a0 = a       // 1. Evaluate getter once and store original reference
a = a0.inc()     // 2. Call inc() on the original reference and reassign to 'a'
val b = a0       // 3. The expression evaluates to the original reference
```

## Operator Overloading and Mutability Contract

Because `++` is resolved via operator overloading, it can be applied to any custom class by defining an `inc()` member or extension function prefixed with the `operator` keyword. The function must return a type that is assignable to the original variable.

Crucially, the `inc()` function must **never** mutate the existing object; it must always return a *new* instance. If `inc()` mutates the internal state and returns `this`, the postfix operator will violate its semantic contract. In the postfix translation (`val a0 = a; a = a0.inc(); val b = a0`), if `a0.inc()` mutates `a0`, the temporary variable `a0` will reflect the mutated state, causing the postfix expression to incorrectly evaluate to the new value instead of the original.

```kotlin theme={"dark"}
class Point(val x: Int, val y: Int) {
    // The operator function MUST return a new instance.
    // Mutating state and returning 'this' breaks postfix semantics.
    operator fun inc(): Point {
        return Point(x + 1, y + 1)
    }
}

var p = Point(0, 0)
p++ // Translates to: val p0 = p; p = p0.inc(); p0
```

## Thread Safety

The `++` operator in Kotlin is **not atomic**. Whether applied to primitives or custom objects, the read-modify-write cycle (`a = a.inc()`) is subject to race conditions in a multithreaded environment. For atomic increments, Kotlin relies on the underlying platform's concurrency utilities (e.g., `AtomicInteger.incrementAndGet()` on the JVM), which bypass the `++` operator syntax entirely.

<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>
