All the excitement about Kotlin is due to the fact that in 2017 Google made it an official language for Android development alongside well known Java. Kotlin is an open source, modern, flexible and concise language that is not actually that new. It dates back to the year 2011 and it was developed by JetBrains, company that makes extremely good IDEs. In case you didn’t know, Kotlin was named after a Russian island located 32 kilometres from St. Petersburg. Kotlin got its first stable release in February 2016 which means this language is well tested and solid.
Kotlin is a statically typed programming language that runs on Java virtual machine. It is highly interoperable with Java and has many useful benefits and advantages. I started using Kotlin a year ago on a small Android app that uses the GitHub API for searching and exploring GitHub repositories. I also wrote the same app in Java so I had great insight about Kotlin benefits which I will describe in this article.
As I said earlier, Kotlin is expressive and concise. Let’s dive into an example. For my GitHub app I had to make a model class that represents Owner data. In Java it looked like this:
Now, let’s compare that to Kotlin:
This data class auto generates all the fields and property accessors. Also, useful methods like toString(), equals(), hashCode() are also generated implicitly. If you need to send object from one activity to another, you don’t have to generate parcelable methods, everything is done with @Parcelize annotation. Cool, right?
Kotlin provides the ability to extend a class with new functionality without having to inherit from the class or use any type of design pattern such as Decorator. This is called extension function. Basically, it is just a method which takes the receiver as its first argument plus additional parameters. Lets see the example from my GitHub explorer app. I wanted to send an Owner object to some parts of the app via bundle. So, I created an extension function that looked like this:
Now, all you have to do is call this methods on your intent. Pretty easy and flexible.
The Kotlin Standard Library comes with some useful extensions out of the box. The one I particularly love is using view ID defined in XML directly from an activity. What does this mean? No more need for findViewById() or using Butterknife.
Everyone is familiar with the frustrating NullPointerException (NPE) – a famous problem in the design of Java language. It’s true that there are a lot of tools and design patterns to avoid this, but they cost time (and money). In Kotlin, if you assign null to property it will give you a compile error. However, if you want to allow null you can do it safely like this:
Explicit null checks are also improved compared to some other languages. For example, this is how we do it in Java:
In Kotlin, an explicit null check is done in single line like this:
You know what is also cool? You can chain multiple null checks. The chain will return null if any of the properties is null. However, if you want to explicitly tell the compiler that a property is not null you can do it like this:
In case the property is null you will get good old NPE. Basically, if you are sure that property is not null use !!. otherwise use?. Also, ?. can be used to return different kinds of exceptions, while !!. can throw only NPE.
Switching to Kotlin is easy for several reasons. One of them is its interoperability with Java. Kotlin runs on JVM and relies on Java code from the existing Java Class Library. You can easily call Java code from Kotlin and vice-versa. If you have existing Java code you can start using Kotlin to get familiar with the language and its constructs until you eventually convert entire codebase to Kotlin. In fact, if you want to convert your Java code to Kotlin, Android Studio gives you the option to do that automatically.
Kotlin will infer your types so you don’t have to explicitly declare it. For example:
Kotlin uses aggressive inference to determine the types of values and expressions if they were not clearly stated. It makes the language even more distinctive from Java which sometimes requires repetitious type specifications.
Better support for functional programming
Functional programming was also introduced in Java 8, but Kotlin has better support for it. In short, functional concepts can be used more concisely and explicitly with proper function types. Also, performance can be improved via inlining which is not currently possible in Java.
Easy to learn, write and explain
When I first saw
in Kotlin it instantly reminded me of Python. Which is great. They are easy to read and flexible:
Another thing that I was completely thrilled about is the when expression which replaces the switch statement:
Kotlin compiler tracks your logic and auto casts types if possible. Remember using
checks followed by explicit casts? Kotlin makes this easy and concise:
Finally, the Kotlin feature I found great is that the == operator actually does what you expect – compares structural equality, where === compares referential equality. For example:
So, what do we get with Kotlin in the Android world? We get mature, open source language developed by JetBrains and backed by Google integrated into official Android IDE. Kotlin is null safe, concise and easy to learn. Apps like Pinterest, Trello, Evernote have already accepted Kotlin as their go-to language for new features. It greatly shortens code by reducing boilerplate and gives you all features every modern language should have. Just open your project and start writing. Happy coding! 🙂
If you want to check out the GitHub explorer project mentioned in this article here are the links:
Kotlin version: https://bitbucket.org/ag04/githubexplorer-example-kotlin
Java version: https://bitbucket.org/ag04/githubexplorer-example-java