Skip to main content

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.

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.
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)
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)
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()
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.
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
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.
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)
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)
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)
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.
val text: String? = "Kotlin"
val length = text?.length ?: 0
val strictLength = text!!.length
Master Kotlin with Deep Grasping Methodology!Learn More