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 in Kotlin is a statically resolved, overloadable operator primarily functioning as a binary addition/concatenation operator or a unary positive operator. Rather than being a hardcoded language construct limited to primitives, the Kotlin compiler translates the + symbol into explicit method calls to functions named plus (for binary operations) or unaryPlus (for unary operations).
Compiler Translation Mechanics
When the Kotlin compiler encounters the+ operator, it performs a desugaring process based on the arity of the operation.
Binary Operation (a + b)
The compiler translates the infix + operator into a call to the plus() function invoked on the left-hand operand, passing the right-hand operand as the argument.
+a)
When used as a prefix, the compiler translates the + operator into a call to the unaryPlus() function invoked on the operand.
Type-Specific Implementations
Because+ relies on function resolution, its behavior is strictly dictated by the type of the left-hand operand:
- Primitives: For numeric types (
Int,Double, etc.), Kotlin maps theplusandunaryPlusfunctions directly to the corresponding JVM bytecode instructions (e.g.,iadd,dadd) to ensure zero runtime overhead. - Strings: When the left operand is a
String,plus(Any?)is invoked. On the JVM, Kotlin optimizes this during compilation usingStringBuilderorStringConcatFactory(depending on the target JVM version) rather than executing literal method calls. - Collections: The Kotlin standard library provides extension functions for
Iterable.plusandCollection.plus. Mechanically, this operator does not mutate the existing collection; it allocates and returns a new read-only collection containing the elements of the original collection followed by the appended element(s).
Operator Overloading Syntax
To enable the+ operator for custom types, you must declare a member or extension function using the operator modifier. The function must be named exactly plus or unaryPlus.
Resolution Rules and Constraints
- Asymmetry: The
plusfunction does not require the parameter type to match the receiver type.a + bis valid as long asahas aplusfunction accepting the type ofb. - Return Type: The return type of the
plusorunaryPlusfunction dictates the type of the resulting expression. It is not constrained to return the same type as the operands. - Precedence: The binary
+operator shares the same precedence level as the binary-operator, which is lower than multiplicative operators (*,/,%) but higher than infix functions and comparison operators. Unary+has a higher precedence than all binary arithmetic operators. - Nullability: The
+operator can be invoked where the left operand is a nullable variable (written simply asa + bwhereais of typeT?) only if an explicit extension function for the nullable type is defined and resolved (e.g.,operator fun Point?.plus(other: Point): Point). Without such an extension, the compiler enforces null safety and rejects the operation.
Master Kotlin with Deep Grasping Methodology!Learn More





