When Google announced in May 2017 that Kotlin would become a first-class language for Android, many developers had never heard of it. Just a little over a year later, more and more developers and companies are using it. Why is it gaining popularity?
Kotlin is the brainchild of JetBrains, the same company behind the popular IntelliJ IDEA IDE. According to the official Kotlin site, companies like Pinterest, Gradle, Uber, Coursera, and Evernote are using Kotlin in some way. The number of Kotlin jobs on job boards is also increasing, so it’s well worth taking a look at the language.
Kotlin and Java
When it comes to learning Kotlin, Java developers have a head start as Kotlin can be compiled to run on the JVM. Not only that, Kotlin and Java are interoperable, meaning that you can use Kotlin and Java in the same application. This means that if you want to transition to Kotlin, you can do so without having to rewrite an entire application. On top of that, you can use the JDK in a Kotlin application, so you can leverage all your existing Java knowledge.
Having said that, Kotlin and Java are different languages. So how does Kotlin differ from Java? Let’s look at a few differences between the two.
Java can sometimes be wordy. Java developers are familiar with classes that look like the following:
To declare a class and provide access to the outside world, you must add a constructor, and setters and getters for the fields you want to expose.
This is so common that the IDEs provide ways to quickly write this boilerplate code. For example, IntelliJ IDEA has generate constructor and generate getter and setter menu items.
Knowing that a class will require a constructor and getters and setters most of the time, Kotlin automatically generates them under the covers. Here is the equivalent class declared in Kotlin:
That’s it. One line of code.
Kotlin is more concise in Java in many other ways. This is only one example.
#2 Null Safety
Java developers write quite a bit of code to prevent null pointer exceptions. Here are few ways that Kotlin helps us avoid the dreaded NullPointerException runtime error.
Nullable vs. non-Nullable Variables
In Kotlin, all variables are non-nullable by default. This means that you can’t assign null to them. You have to declare a nullable variable in order to assign null, something you’ll try to avoid doing.
When you declare a non-nullable variable, you can use it with full confidence that it will never contain null, which means you don’t have to write code to check for null – another way Kotlin can be more concise.
Safe Call Operator
Kotlin has a safe call operator that returns a value when a variable is not null, or null when it is. For example, look at this code, which is common in Java:
In Kotlin, there’s no need to explicitly test for null. You can write the following:
If a or b or c is null, the expression will short circuit at that point, and the null safety operator (?.) will return null. A NullPointerException will not be thrown. If none of them are null, then a.b.c.color will be returned.
These are just a couple of ways that null safety is built into Kotlin. There are more.
In Java, we often override a class because we want to add a custom method. There’s an easier way to do it in Kotlin. We can use an extension function.
For example, let’s say you want to know whether the length of a String is equal to 4. If you want to perform this test a lot, you can add an extension function that performs the test, as follows:
If you’ve never seen Kotlin, this might look strange. The keyword fun is shorthand for function. String tells the compiler that you’re adding an extension function that you can invoke using String objects. In this case, String is the receiver type. isFourCharacters is the function name. The function doesn’t take any arguments, and it returns a Boolean.
Because the function has only one line of code and returns a value, you don’t have to use curly braces around the body of the function. Instead, you can simply assign the result of the conditional statement to the function, which effectively returns the result from the function. This is another feature in Kotlin.
Inside an extension function, you refer to the String you’re receiving, or the receiver object, using the keyword this.
To call the extension function, you’d do the following:
In this case, “bear” is the receiver object. Inside the function, it will be substituted for this. Since it is four characters in length, the function will return true.
When you use extension functions, you aren’t actually extending the class, so this doesn’t violate encapsulation. The compiler does some magic under the covers to make it work.
In Java, we often write code like the following:
We have to test whether an object is of a specific type before we cast to that type.
In Kotlin, you can write this more concisely:
Notice that you don’t have to cast to a String when you access the length property. Kotlin has the notion of smart casting. If the compiler can be certain that the value of a variable won’t change from the time you check its type with the is operator, to the time you use it, then there’s no need to explicitly cast. If you wanted to, you could have more statements within the if block that call String methods on obj, and you wouldn’t have to cast to String.
We’ve touched on a handful of differences between Kotlin and Java, to give you a taste of the flavour of Kotlin. There are many more differences, and Kotlin has added functionality that Java doesn’t have. But remember that Kotlin is completely interoperable with Java. You don’t have to spend months learning Kotlin, or months (years) rewriting an application, before you can start to use the language. You can add Kotlin classes to your existing Java applications. You can also leverage all your existing knowledge of the JDK.
If you’ve been wanting to give Kotlin a try, now is a great time to jump in and learn it. Google has blessed it, and companies are looking for developers who have Kotlin in their toolbox.