Search⌘ K
AI Features

Overloading Operators

Explore how to overload operators in Kotlin to write fluent and expressive code. Understand the benefits and risks, learn the mapping of operators to special functions, and discover practical guidelines to implement operator overloading correctly for user-defined classes.

Need for operator overloading

Traditionally we use operators on numeric types to create expressions, for example, 2+32 + 3 or 4.27.14.2 * 7.1. Operator overloading is a feature where the language extends the capability to use operators on user-defined data types.

Compare the two lines in the following code snippet:

bigInteger1.multiply(bigInteger2) 

bigInteger1 * bigInteger2

The first line uses the ...