Operator Overloading

Operator overloading lets you give symbols like + , [ ] , and < a custom meaning for your own types by writing specially-named functions marked with the operator keyword.

Learn Operator Overloading in our free Kotlin course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Kotlin course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

You'll overload arithmetic, indexing, comparison, and the call operator, building a clean Vector2 type along the way.

What You'll Learn in This Lesson

1️⃣ Arithmetic: plus, minus, times

Each arithmetic symbol maps to a fixed function name: + → plus , - → minus , * → times . Mark the function operator and Kotlin rewrites a + b into a.plus(b) .

2️⃣ Indexing with get and set

The square-bracket syntax is two operators. Reading g[r, c] calls get(r, c) ; writing g[r, c] = v calls set(r, c, v) . You can take any number of index arguments, which is perfect for a 2-D grid.

The set on a cell never touched returns the array's default 0 — handy for sparse data.

3️⃣ Comparison and invoke

A single compareTo returning an Int backs all four of < , > , <= , >= : negative means less, zero equal, positive greater. Separately, invoke lets you call an object like a function.

Your turn. Fill in the ___ blank, then run and compare.

Overload plus and times so volumes add and scale naturally.

📋 Quick Reference — Operator → Function

Practice quiz

Which function name backs the + operator?

  • add
  • plus
  • sum
  • concat

Answer: plus. a + b is rewritten as a.plus(b), so + is backed by the plus function.

What keyword must mark a function so it can back an operator?

  • override
  • infix
  • operator
  • open

Answer: operator. You must mark the specially-named function with the operator keyword.

What does a[i] = v call under the hood?

  • a.get(i, v)
  • a.put(i, v)
  • a.assign(i, v)
  • a.set(i, v)

Answer: a.set(i, v). Indexed assignment a[i] = v is backed by the set function.

How many functions do you implement to support <, >, <=, and >=?

  • One compareTo function returning Int
  • Four separate functions
  • Two functions: less and greater
  • None, they come free

Answer: One compareTo function returning Int. A single compareTo returning an Int powers all four comparison operators.

What should compareTo return when the receiver is less than the argument?

  • A positive number
  • true
  • Zero
  • A negative number

Answer: A negative number. Negative means less, zero means equal, and positive means greater.

What does the invoke operator let you do?

  • Compare two objects
  • Index into an object with brackets
  • Call an object like a function, as in obj(args)
  • Add two objects

Answer: Call an object like a function, as in obj(args). invoke makes an object callable, so obj(x) calls obj.invoke(x).

Which function name backs the * operator?

  • mul
  • product
  • times
  • multiply

Answer: times. a * b is rewritten as a.times(b).

How do most classes get a correct == for free?

  • By overloading equals manually
  • By being a data class
  • By implementing Comparable
  • By marking the class operator

Answer: By being a data class. A data class generates a correct equals (and hashCode), backing ==.

What happens if you name the function pluss instead of plus?

  • + still works
  • It backs the ++ operator
  • The compiler reports an unresolved reference for +
  • It backs the += operator

Answer: The compiler reports an unresolved reference for +. Only the exact predefined names work; pluss does not back any operator.

When is operator overloading a good idea?

  • Whenever it saves typing
  • When the symbol's meaning is obvious and matches intuition
  • Only for collection types
  • Never, it is always discouraged

Answer: When the symbol's meaning is obvious and matches intuition. Overload only when the operator's meaning is clear, like + on vectors or < on money.