Chapter 3 : Working with Operators in Kotlin (Android)

In this article, you will going to learn about operators and its usage in Kotlin.

But before moving to current topic, I explained how you can configure your Android Studio with Kotlin and working with variables in previous articles. If you are still pending with that, please read below article first and then continue with this article.

Chapter 1 : Configuring Android Studio with Kotlin
Chapter 2 : Working with Variables in Kotlin (Android)

Binary Operators

Kotlin is very powerful language in terms of using operators. Other languages like Java, are not allowing user to use arithmetic operators except primitive types. If you want to use + operator for ArrayList, you are not allowed to do that. But with Kotlin, it is possible to do so. Kotlin provides binary operators’ corresponding function names which can be overloaded.

Expression Function
a + b a.plus(b)
a - b a.minus(b)
a * b a.times(b)
a / b a.div(b)
a % b a.rem(b), a.mod(b) (deprecated)
a..b a.rangeTo(b)

Note
If you are using Kotlin 1.1, use rem() function as mod() is deprecated in from 1.1.

Here you can see that, for each binary operator a function is provided to read it more clearly. In case you want to use arithmetic function to your custom class, you can easily use it and overload it.

Interesting!

Unary operations

You must be familiar with unary operators and its use in Java or other language. You can also use this unary operators in your custom class and overload it as per your choice.

Expression Function
+a a.unaryPlus()
-a a.unaryMinus()
!a a.not()

Here, you can see that unary function has no argument. When -test is called, it will simply check whether unaryMinus() is overloaded or not. If yes, it will provide you appropriate result.

Increment and Decreament

Your for loop is not full without this operator. Yes, ++ and — are very much helpful operators. Below are functions you can use to overload it.

Expression Function
a++ a.inc()
a-- a.dec()

Assignments Operators

Using assignment operator, you can change value of calling object by performing binary operation. While using simple binary operation calling object’s value won’t change.

Expression Function
a += b a.plusAssign(b)
a -= b a.minusAssign(b)
a *= b a.timesAssign(b)
a /= b a.divAssign(b)
a %= b a.modAssign(b)

Rest of operators

There are many other operators which you can use.

Expression Function
a in b b.contains(a)
a !in b !b.contains(a)
a[i] a.get(i)
a[i, j] a.get(i, j)
a[i_1, ..., i_n] a.get(i_1, ..., i_n)
a[i] = b a.set(i, b)
a[i, j] = b a.set(i, j, b)
a[i_1, ..., i_n] = b a.set(i_1, ..., i_n, b)
a == b a?.equals(b) ?: (b === null)
a != b !(a?.equals(b) ?: (b === null))
a() a.invoke()
a(i) a.invoke(i)
a(i, j) a.invoke(i, j)
a(i_1, ..., i_n) a.invoke(i_1, ..., i_n)
a > b a.compareTo(b) > 0
a < b a.compareTo(b) < 0
a >= b a.compareTo(b) >= 0
a <= b a.compareTo(b) <= 0

Try above operators by creating your own example and share your experience with it.

Summary

In this article, you learnt how you can use operators and how to overload function with primitive as well custom class.

Don’t forget to share this article and your feedback.

Protected by Copyscape

Share on Facebook12Share on Google+12Tweet about this on TwitterShare on LinkedIn0Share on Reddit0Pin on Pinterest0Share on Tumblr0Email this to someone