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 theDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
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 theoperator keyword and use a predefined function name.
Arithmetic Operators
Standard arithmetic operators translate to specific mathematical functions.a + btranslates toa.plus(b)a - btranslates toa.minus(b)a * btranslates toa.times(b)a / btranslates toa.div(b)a % btranslates toa.rem(b)
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 += btranslates toa.plusAssign(b)ORa = a.plus(b)a -= btranslates toa.minusAssign(b)ORa = a.minus(b)a *= btranslates toa.timesAssign(b)ORa = a.times(b)a /= btranslates toa.divAssign(b)ORa = a.div(b)a %= btranslates toa.remAssign(b)ORa = a.rem(b)
Unary Operators
Unary operators operate on a single operand and translate to parameterless functions.+atranslates toa.unaryPlus()-atranslates toa.unaryMinus()!atranslates toa.not()
++a/a++translates toa.inc()--a/a--translates toa.dec()
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 == btranslates toa?.equals(b) ?: (b === null)a != btranslates to!(a?.equals(b) ?: (b === null))
===, !==):
Evaluates whether two references point to the exact same object in memory. This operator is evaluated directly by the compiler and cannot be overloaded.
Relational Operators
Relational operators are used for comparison and translate to thecompareTo() function, which must return an Int.
a > btranslates toa.compareTo(b) > 0a < btranslates toa.compareTo(b) < 0a >= btranslates toa.compareTo(b) >= 0a <= btranslates toa.compareTo(b) <= 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 istrue.||(Logical OR): Evaluates the right operand only if the left isfalse.
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)
Collection and Range Operators
These operators facilitate interactions with collections, arrays, and ranges. In Operator:a in btranslates tob.contains(a)a !in btranslates to!b.contains(a)
a[i]translates toa.get(i)a[i, j]translates toa.get(i, j)a[i] = btranslates toa.set(i, b)
a..btranslates toa.rangeTo(b)a..<btranslates toa.rangeUntil(b)
Invoke Operator
Parentheses applied to an object instance translate to theinvoke() function.
a()translates toa.invoke()a(i)translates toa.invoke(i)
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 aNullPointerExceptionif the value is null.
Master Kotlin with Deep Grasping Methodology!Learn More





