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

Operators in Kotlin are special symbols or keywords that perform specific operations on one or more operands. At the compiler level, Kotlin implements most operators as syntactic sugar for underlying method calls. This mechanism allows custom types to define operator behavior using the `operator` modifier, a feature known as operator overloading.

## Operator Overloading Mechanics

When an operator is used in code, the Kotlin compiler translates it into a corresponding member or extension function. To overload an operator for a custom class, the function must be prefixed with the `operator` keyword and use a predefined function name.

```kotlin theme={"dark"}
class Vector(val x: Int, val y: Int) {
    operator fun plus(other: Vector): Vector {
        return Vector(this.x + other.x, this.y + other.y)
    }
}

val v1 = Vector(1, 2)
val v2 = Vector(3, 4)
val v3 = v1 + v2 // Translates to v1.plus(v2)
```

## Arithmetic Operators

Standard arithmetic operators translate to specific mathematical functions.

* `a + b` translates to `a.plus(b)`
* `a - b` translates to `a.minus(b)`
* `a * b` translates to `a.times(b)`
* `a / b` translates to `a.div(b)`
* `a % b` translates to `a.rem(b)`

```kotlin theme={"dark"}
val sum = 10 + 5       // 10.plus(5)
val remainder = 10 % 3 // 10.rem(3)
```

## Augmented Assignment Operators

Compound assignment operators combine an arithmetic operation with assignment. The compiler resolves these by first looking for a specific assignment function (e.g., `plusAssign`). If that function is not available or the variable is immutable (`val`), it falls back to standard arithmetic reassignment.

* `a += b` translates to `a.plusAssign(b)` OR `a = a.plus(b)`
* `a -= b` translates to `a.minusAssign(b)` OR `a = a.minus(b)`
* `a *= b` translates to `a.timesAssign(b)` OR `a = a.times(b)`
* `a /= b` translates to `a.divAssign(b)` OR `a = a.div(b)`
* `a %= b` translates to `a.remAssign(b)` OR `a = a.rem(b)`

```kotlin theme={"dark"}
var total = 10
total += 5 // Translates to total = total.plus(5)

val list = mutableListOf(1, 2)
list += 3  // Translates to list.plusAssign(3)
```

## Unary Operators

Unary operators operate on a single operand and translate to parameterless functions.

* `+a` translates to `a.unaryPlus()`
* `-a` translates to `a.unaryMinus()`
* `!a` translates to `a.not()`

**Increment and Decrement:**
These operators mutate the variable and return a value depending on whether they are used as a prefix or postfix.

* `++a` / `a++` translates to `a.inc()`
* `--a` / `a--` translates to `a.dec()`

```kotlin theme={"dark"}
var count = 0
count++ // count = count.inc()

val isTrue = false
val isFalse = !isTrue // isTrue.not()
```

## Equality Operators

Kotlin distinguishes between structural equality and referential equality.

**Structural Equality (`==`, `!=`):**
Evaluates the content or state of the operands. It is null-safe and translates to the `equals()` function.

* `a == b` translates to `a?.equals(b) ?: (b === null)`
* `a != b` translates to `!(a?.equals(b) ?: (b === null))`

**Referential Equality (`===`, `!==`):**
Evaluates whether two references point to the exact same object in memory. This operator is evaluated directly by the compiler and cannot be overloaded.

```kotlin theme={"dark"}
val obj1 = String("Kotlin".toCharArray())
val obj2 = String("Kotlin".toCharArray())

val isEqual = (obj1 == obj2)   // true (Structural)
val isSameRef = (obj1 === obj2) // false (Referential)
```

## Relational Operators

Relational operators are used for comparison and translate to the `compareTo()` function, which must return an `Int`.

* `a > b` translates to `a.compareTo(b) > 0`
* `a < b` translates to `a.compareTo(b) < 0`
* `a >= b` translates to `a.compareTo(b) >= 0`
* `a <= b` translates to `a.compareTo(b) <= 0`

```kotlin theme={"dark"}
val isGreater = 5 > 3 // 5.compareTo(3) > 0
```

## Logical Operators

Logical operators perform boolean logic. The binary logical operators utilize short-circuit evaluation and cannot be overloaded.

* `&&` (Logical AND): Evaluates the right operand only if the left is `true`.
* `||` (Logical OR): Evaluates the right operand only if the left is `false`.

```kotlin theme={"dark"}
val a = true
val b = false
val c = true
val condition = (a && b) || c // Evaluates to true
```

## Bitwise Operators

Unlike C-style languages, Kotlin does not use special symbols (like `&`, `|`, `<<`) for bitwise operations. Instead, it uses named infix functions that operate on `Int` and `Long` types.

* `shl` (Signed shift left)
* `shr` (Signed shift right)
* `ushr` (Unsigned shift right)
* `and` (Bitwise AND)
* `or` (Bitwise OR)
* `xor` (Bitwise XOR)
* `inv()` (Bitwise inversion/NOT - this is a standard function, not infix)

```kotlin theme={"dark"}
val bitmask = 0b1100 and 0b1010 // Result: 0b1000
val shifted = 1 shl 3           // Result: 8
```

## Collection and Range Operators

These operators facilitate interactions with collections, arrays, and ranges.

**In Operator:**

* `a in b` translates to `b.contains(a)`
* `a !in b` translates to `!b.contains(a)`

**Indexed Access:**

* `a[i]` translates to `a.get(i)`
* `a[i, j]` translates to `a.get(i, j)`
* `a[i] = b` translates to `a.set(i, b)`

**Range Operators:**

* `a..b` translates to `a.rangeTo(b)`
* `a..<b` translates to `a.rangeUntil(b)`

```kotlin theme={"dark"}
val array = intArrayOf(10, 20, 30)
val exists = 5 in 1..10 // (1..10).contains(5)
val item = array[0]     // array.get(0)
array[1] = 99           // array.set(1, 99)
```

## Invoke Operator

Parentheses applied to an object instance translate to the `invoke()` function.

* `a()` translates to `a.invoke()`
* `a(i)` translates to `a.invoke(i)`

```kotlin theme={"dark"}
class Factory {
    operator fun invoke(): String = "Instance created"
}
val factory = Factory()
val result = factory() // factory.invoke()
```

## Null Safety Operators

Kotlin provides specific operators handled directly by the compiler to manage nullability. These cannot be overloaded.

* `?.` (Safe Call): Executes the call only if the receiver is not null; otherwise, returns null.
* `?:` (Elvis Operator): Returns the left operand if it is not null; otherwise, evaluates and returns the right operand.
* `!!` (Not-Null Assertion): Casts the operand to a non-null type, throwing a `NullPointerException` if the value is null.

```kotlin theme={"dark"}
val text: String? = "Kotlin"
val length = text?.length ?: 0
val strictLength = text!!.length
```

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