In 2017 Google announced first-class support for Kotlin on Android at Google I/O, and shortly after I started looking into using Kotlin for our Android projects here at Shockoe. It took a few years before we officially started using Kotlin as our preferred language for Android development, but I now believe that using Kotlin instead of Java can make writing Android code easier, faster, and less prone to errors. I am going to list some of my favorite features of Kotlin that have improved my workflow.

1. Using val and var effectively in Kotlin, variables are declared with either the val or var keywords. Vals cannot be reassigned (like variables declared as final in Java) while vars can be reassigned after creation. If you need to reassign the value of a variable, you will have to declare it as a var. If you do not need to reassign the variable, you can declare it as a val. Kotlin can use static checks to perform common tasks automatically. Let’s take the example where the type of a variable can vary at runtime, and you want to perform different tasks based on the object’s type. If you use a var it might look like this. The var has to be cast to a String before you can perform any String functions on it.

When using a val, Kotlin can automatically cast the variable to a string inside the if statement

One goal the creators of Kotlin have is to eliminate NullPointer exceptions (NPEs) from our code. Kotlin’s type system has references that can hold null (nullable references) and references that can not (non-null references). Kotlin offers a few different tools for dealing with nullable references. If the reference is a val, it can be autocast to be non-null using an if statement, similar to the example in the last section. There is also the Safe Call option, written as ?., which will return the result of the expression if the reference is non-null or will return null if any reference in the chain is null. The next option is the Elvis Operator, written as ?:, which will return the result of the left side of the expression if it is not null, otherwise it returns a non-null reference that you specify. A good example of where to use this is in a RecyclerView Adapter that uses a nullable reference to store its data:

The last option is the !! operator. This is also the least safe option, because it is a non-null assertion operator. This operator can be dangerous because it will actually throw a NPE if it is called on a null value. This option should only be used when necessary because of the risks involved.

3. One of my favorite features of Kotlin is extension functions. These let you easily add new functionality to a class without having to inherit from it. This can be especially useful for Android development where it is difficult or cumbersome to inherit from classes like Context. Here is an example extension function that can be used to convert values from density-independent pixels to pixels.

I found that is is much easier to write extension functions for simple tasks like this, instead of having one big file with static functions. This function can also be used on any instance of Context or any of its descendants.

4. Functions in Kotlin are first class. They can be stored in variables and passed to other functions as parameters. I have found that storing function in variables is a good alternative to writing a Java interface that only has one method in it, such as a click listener:

5. Kotlin is interoperable with Java. Kotlin bytecode is indistinguishable from Java bytecode, and therefore the two can run alongside each other. If you have an existing project written in Java, you don’t need to rewrite all your existing classes to experience all the benefits of Kotlin. In one project, I had many classes where their main functionality was to hold data. I started by converting these classes to Kotlin Data Classes. With data classes, the compiler automatically creates some common functions: equals(), hashCode(), toString(), copy().

I think that Kotlin has some great features that can make developing faster and easier. JetBrains and Google have created some great tools for starting a project with Kotlin or adding Kotlin support to an existing Java project. Android Studio has an option to convert an existing Java file to Kotlin, and in my experience, it has worked pretty well. Because of Kotlin’s recent growth in the development community and the support provided by JetBrains and Google, I believe this is a perfect time to start using Kotlin if you have been at all curious about it.

Sign up for the Shockoe newsletter and we’ll keep you updated with the latest blogs, podcasts, and events focused on emerging mobile trends.