Search⌘ K
AI Features

Operator Overloading

Explore how Kotlin allows you to overload common operators like plus and minus by defining functions with the operator keyword. Understand how operator overloading enables concise syntax for tasks such as adding vectors or modifying collections. This lesson helps you grasp Kotlin’s advanced syntax for more readable and efficient code.

Kotlin operators under the hood

Kotlin has a wide range of operators. Under the hood, these existing operators translate into member function calls. Therefore, in Kotlin, a+b is really a.plus(b), -a is the a.unaryMinus() call, and a++ is in fact a.inc().

Overloading Kotlin operators

Kotlin supports overloading existing operators (like +, -, + =, and ...