operator modifier, which allows classes to define or overload operator behavior by providing specifically named member or extension functions.
Operator Overloading Mechanism
When the compiler encounters an operator, it resolves it to a corresponding function call. For example, the expressiona + b is translated to a.plus(b). To enable this for custom types, the function must be prefixed with the operator keyword.
Operator Categories and Function Mapping
Unary Prefix Operators
These operate on a single operand and are evaluated before the expression.| Expression | Translated Method Call |
|---|---|
+a | a.unaryPlus() |
-a | a.unaryMinus() |
!a | a.not() |
Increments and Decrements
These operators mutate the operand. The compiler handles the assignment automatically; the overloaded function must return the new value, not mutate the object in place.| Expression | Translated Method Call |
|---|---|
a++ / ++a | a.inc() |
a-- / --a | a.dec() |
++a) return the updated value. Postfix forms (a++) store the initial value, perform the inc() operation, assign the result to a, and return the stored initial value.
Arithmetic Operators
Binary operators that perform standard mathematical computations.| Expression | Translated Method Call |
|---|---|
a + b | a.plus(b) |
a - b | a.minus(b) |
a * b | a.times(b) |
a / b | a.div(b) |
a % b | a.rem(b) |
Augmented Assignments
These combine an arithmetic operation with assignment. The compiler resolves expressions likea += b based on the availability of specific assignment functions (e.g., plusAssign) versus standard arithmetic functions (e.g., plus).
- If
plusAssignis defined, it translates toa.plusAssign(b). - If
plusAssignis not defined, butplusis defined andais a mutable variable (var), it translates toa = a.plus(b). - If both are defined and
ais mutable, the compiler reports anAssignment operators ambiguityerror to prevent unpredictable mutation behavior. | Expression | Translated Method Call (if specific assignment exists) | | :--- | :--- | |a += b|a.plusAssign(b)| |a -= b|a.minusAssign(b)| |a *= b|a.timesAssign(b)| |a /= b|a.divAssign(b)| |a %= b|a.remAssign(b)|
Range Operators
These operators create ranges or progressions between two values.| Expression | Translated Method Call |
|---|---|
a..b | a.rangeTo(b) |
a..<b | a.rangeUntil(b) |
Equality and Inequality
Kotlin distinguishes between structural equality (==) and referential equality (===).
Structural Equality:
Translated to the equals() function. The compiler injects null-safety checks during translation.
| Expression | Translated Method Call |
|---|---|
a == b | a?.equals(b) ?: (b === null) |
a != b | !(a?.equals(b) ?: (b === null)) |
=== and !==) are intrinsic to the JVM/runtime and cannot be overloaded.
Comparison Operators
Translated to thecompareTo method, which must return an Int adhering to the standard contract (negative if less, zero if equal, positive if greater).
| Expression | Translated Method Call |
|---|---|
a > b | a.compareTo(b) > 0 |
a < b | a.compareTo(b) < 0 |
a >= b | a.compareTo(b) >= 0 |
a <= b | a.compareTo(b) <= 0 |
Collection and Access Operators
Kotlin provides operators for membership checking and indexed access.| Expression | Translated Method Call |
|---|---|
a in b | b.contains(a) |
a !in b | !b.contains(a) |
a[i] | a.get(i) |
a[i, j] | a.get(i, j) |
a[i] = b | a.set(i, b) |
Invoke Operator
Parentheses translate to theinvoke function, allowing instances to be called as if they were functions.
| Expression | Translated Method Call |
|---|---|
a() | a.invoke() |
a(i) | a.invoke(i) |
Destructuring Declaration Operators
Kotlin allows unpacking an object into multiple variables. This syntax is powered by sequentially numberedcomponent functions.
| Expression | Translated Method Call |
|---|---|
val (x, y) = a | val x = a.component1()val y = a.component2() |
Iterator Operator
For an object to be iterable within afor loop, it must provide an iterator operator function. The returned iterator object must subsequently provide next() and hasNext() operator functions.
| Expression | Translated Method Call |
|---|---|
for (item in a) | val it = a.iterator()while (it.hasNext()) { val item = it.next() } |
Property Delegation Operators
Theby keyword delegates the getter (and setter) of a property to another object. The delegate object must provide getValue and, for mutable properties, setValue operator functions. An optional provideDelegate operator can also be defined to intercept the delegation creation.
| Expression | Translated Method Call |
|---|---|
val p by d | d.getValue(thisRef, property) |
var p by dp = value | d.getValue(thisRef, property)d.setValue(thisRef, property, value) |
Bitwise Operations
Unlike C-style languages, Kotlin does not use symbolic operators (like&, |, <<) for bitwise operations. Instead, it utilizes named functions combined with the infix modifier, allowing them to be called without dot notation or parentheses.
shl (signed shift left), shr (signed shift right), ushr (unsigned shift right), and, or, xor, and inv (bitwise inversion, called as a standard method).
Tired of Poor Kotlin Skills? Fix That With Deep Grasping!Learn More





