Over a million developers have joined DZone.

Learning Kotlin: Operators

DZone 's Guide to

Learning Kotlin: Operators

The Kotlin programming language provides extensive support for your classes. Check out this post to learn more about how to use operators in Kotlin.

· Java Zone ·
Free Resource

This next post is an introduction to operators in Kotlin, but we are not talking about the basic "use a plus sign to add numbers together" stuff. No. This post is about how amazingly extensive the language is when it comes to support and allowing your classes to use them.

Adding Things Together

So, let's start with a simple addition of all the necessary components. In the following example code, we created a way to keep track of scoring events in a rugby game, and I would like to add up those events to get the total score:

enum class scoreType {

data class score(val type: scoreType) {
    fun points() = when(this.type) {
        `try` -> 5
        scoreType.conversion -> 2
        scoreType.kick -> 3

fun main(args: Array <String> ) {
    val gameData = listOf(score(scoreType.
        `try`), score(scoreType.conversion), score(scoreType.
        `try`), score(scoreType.kick))
    var totalPoints = 0
    for (event in gameData) {
        totalPoints = event + totalPoints


This obviously won't compile, you can't += an Int  and my own class. We could make this change, which is probably the better way, but, for my example, let's say that this isn't ideal.

totalPoints = event.points() + totalPoints

So, to get our code to compile, we just need a function named plus, which has the operator keyword, and while I could call this myself, the Kotlin compiler is smart enough to make it work:

data class score(val type:scoreType) {
fun points() = when(this.type) {
scoreType.`try` -> 5
scoreType.conversion -> 2
scoreType.kick -> 3

operator fun plus(other:Int) = this.points() + other

How cool is that?!

If I wanted to take it further and support say totalPoints += event, then, you would need to add a function to the Integer, which tells it how to add a score to it. Thankfully, that is easy with extension functions:

operator fun Int.plus(other:score) = this + other.points()


While the above is a bit silly, imagine building classes for distances, time, weights, etc. This will allow us to have a kilogram and a pound class and add them together! What makes Kotlin shine is how extensive it is, just look at this list!

Class Operators Method Example Expression
Arithmetic +, '+=' plus first + second
Augmented Assignments += plusAssign |first += second`
Unary + unaryPlus +first
Increment & Decrement ++ inc first++
Arithmetic -, -= minus first - second
Augmented Assignments -= minusAssign first -= second
Unary - unaryMinus `-first
Increment & Decrement -- dec first--
Arithmetic *, *= times first * second
Augmented Assignments *= timesAssign first *= second
Arithmetic /, /= div first / second
Augmented Assignments /= divAssign first /= second
Arithmetic %, %= rem first % second
Augmented Assignments %= remAssign first %= second
Equality ==, != equals first == second
Comparison >, <, <=, >= compareTo first > second
Unary ! not !first
Arithmetic .. rangeTo first..second
In in, !in contains first in second
Index Access [, ] get This returns a value | first[index]
Index Access [, ] set This sets a value | first[index] = second
Invoke () invoke first()

I am going to go through some of these in greater detail in future blog posts, but there is one that I wanted to call out now:

Augmented Assignments vs. Plus or Minus

You might wonder why, when we just implemented plus above, we got both support for + and +=. And, you may be wondering why the table lists += under both plus and augmented were given support. So, why both? This is because you may want to support just += without supporting +.

java ,kotlin ,tutorial ,operators ,class ,int ,augmented assignments

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}