5 Reasons Why I Love Kotlin

Kotlin is a language which was developed by the guys at JetBrains: They’re the guys responsible for bringing you IntelliJ, and by that extension Android Studio.

At Google I/O 2017, Google announced that Kotlin is officially a supported Android development language. You were able to use Kotlin for Android development, but now it’s an official language of the Android SDK.

I’ve never used Kotlin up until that point. I have heard of it, but I haven’t used it. I watched the Introduction To Kotlin video from the Google I/O. The presenter goes over the basics of Kotlin, showing it in use compiled down to bytecode and run on the JVM. He showed all of the basic features, and compares it to Java, since Java has been the only officially supported language for Android development.

That video peeked my interest. Kotlin seemed to be a really fun language. I decided to try it out.

Android Studio makes it really easy to add Kotlin to your app. It just took a single mouse click and a gradle sync. Then you are off to the races.

I started learning about it mainly through converting existing Java code into Kotlin.

I’ve been using Kotlin as my primary Android Development language ever since then. And I love it.

Because of that I made this list of my top 5 reasons why I love Kotlin.

1: Interoperability with Java

Kotlin can be compiled and run in a couple of ways. Since my primary interest is Android development, I am mostly interested in the bytecode and JVM portion of it.

You can seamlessly integrate a little Kotlin snippet in your Android code, since it shouldn’t affect the rest of your code. You can still run your existing Java classes.

The official Kotlin website wrote a couple of examples on calling Java from your Kotlin file, and calling Kotlin from your Java file.

2: Null safety

One of the biggest Kotlin features which was mentioned at the Google I/O 2017 is the null safety.

Java is pretty notorious for it’s NullPointerException, also known as the billion-dollar mistake. Kotlin aims to get rid of the infamous exception with it’s safe calls and non-null values.

Safe calls, using the ? operator will return the non-null value or null.

The !! operator is, in the words of the guys at Kotlin, for the NullPointerException lovers. It return the non-null value of throws a NullPointerException.

3: Extension functions

Extension functions give Kotlin functionality of C# in a sense that you can add additional functionality to a class without the need to create a new class which extends the same class. You simply write an extension function. This is one of my favourite Kotlin features.

I’ve used this features many times, and it makes life so much easier. Here is an example of using an extension function.

4: No boilerplate code

Android SDK supports all of Java 7 features and only a few Java 8 features. The biggest Java 8 feature, at least in my opinion, is the addition of lambda. Lambda makes  life so much easier.

Unfortunately, lambdas are not one of those supported Java 8 features.

Now, Kotlin is a different story. Kotlin also features lambdas. And you can use them with Android. This is the best part about Kotlin Android development for me.

Let’s look at an example. We’ll take something that we use every day in Android development and show how to implement that feature in Java and in Kotlin. We’ll take a look at setting a onClickListener.

Java:

Kotlin:

Now, I know that I put everything in one line (parenthesis, the Todo and call of the method), but often time your onClick method only needs to execute one line of code, and this looks much cleaner than adding a new row for a single parenthesis.

A little bonus feature, I love using Kotlin with RxJava. It simplifies the code evan more.

5: Data classes

Data classes are used all of the time for many things. Most of the time, you’ll see data classes used when creating a database. You need a custom Data structure, so you create your own custom data class.

Here is a comparison of a Java data class, and a Kotlin one.

Java:

Kotlin:

Kotlin here has a clear advantage. Sure data classes are not anything difficult to write, but it can get really boring. You’re practically writing the same function over and over again.

And if you’re wondering how do you the access the variables than if there’s no getters and setters?

Kotlin has these things called properties. You would access these variables like this:

And you would access this Kotlin data class from a Java class like this:

Summary

I really, really love Kotlin and I’m currently writing my project app completely in Kotlin.