Kotlin On Android – Why You Should Know About It

Java has been the primary way of making Android apps since the very beginning. Also, Java has been the only officially supported language that has access to the UI elements. Of course NDK is also a thing, but as I said, Java was the only language with the access to the UI elements for a long time. Until a week ago. At the Google IO 2017, Google announced that Kotlin is an officially supported language for Android development. You could write Android apps in Kotlin evan before the IO, but now it’s an official language of Android.

What is Kotlin

Kotlin is a language that is built to run on the Java Virtual Machine. There is a bunch more of languages that run on the JVM, for example Scala and Groovy.

Kotlin s built to be easy to learn. Especially if you already know a programming language. Also Kotlin is very similar to Java, so if you’re already an Android developer this should not be a problem for you.

Comparing Kotlin to Java

When you first create a project in Android Studio you’re greeted with this bit of code in Java:

This is the same code written in Kotlin:

As you see, it’s pretty similar. Also it’s a little bit more compact. It reminds me a little bit of JavaScript.

Here’s another comparison:

And in Kotlin:

As you may have noticed, there are no semi columns in Kotlin. Semi columns in Kotlin are optional. As Hadi Hariri said in this video: Introduction to Kotlin (Google I/O ’17), the semi columns are optional so that we can have endless debates as to why we would need them.

Converting Java to Kotlin

Open MainActivity.java file. Then invoke action Convert Java File to Kotlin File. You can do it by several ways. The easiest one is to invoke Find Action and start typing an action name (like in a screencast below). Alternatively you can call this option via the Code | Convert Java File to Kotlin File menu entry or by using the corresponding shortcut (you can find it at the menu entry).

 

 

 

 

 

 

Resources for learning Kotlin

As I said a couple of times already, Kotlin is a very easy language to learn, especially if you already know Java and Android. So here are a couple of links for getting into Kotlin:

Getting Started With Kotlin

Kotlinlang.org

Treehouse course

Kotlin for Android Developers

O’Reilly course

Have fun learnig!