Skip to main content
The + operator in Kotlin is a statically resolved, overloadable operator. At compile time, the Kotlin compiler translates the + expression into a direct method call to a corresponding function invoked on the left-hand operand (the receiver), passing the right-hand operand as the argument.

Lexical Translation

The compiler maps the + token to specific function names depending on its arity (unary or binary):
  • Binary operation: a + b is translated to a.plus(b)
  • Unary operation: +a is translated to a.unaryPlus()

Operator Overloading Mechanics

Kotlin allows custom types to define their own behavior for the + operator. To enable this, a class or an extension function must declare a function named exactly plus (or unaryPlus) and prefix it with the operator modifier. Without the operator keyword, the compiler will not map the + symbol to the function.

Type Resolution and Signatures

The resolution of the + operator is strictly determined by the static type of the receiver. The right-hand operand does not need to match the type of the left-hand operand, provided a matching plus() signature exists. Furthermore, the return type is entirely dictated by the function signature.

Immutability Convention

By convention and standard library design, the + operator is non-mutating. It does not alter the state of either the receiver or the argument. Instead, it allocates and returns a new object representing the combined result.

Standard Library Implementations

The Kotlin standard library provides built-in operator fun plus implementations for several core types:
  • Primitives: Mapped to JVM-level arithmetic addition instructions (e.g., IADD, DADD).
  • Strings: On JVM 9+ targets (since Kotlin 1.5.20), string concatenation is compiled using invokedynamic (specifically StringConcatFactory) to optimize performance and memory allocation.
  • Collections: Implemented as extension functions that allocate a new read-only collection. This new collection contains the elements from the left operand followed by the right operand, where the right operand can be either a single element or another collection.
Tired of Poor Kotlin Skills? Fix That With Deep Grasping!Learn More