In this blog post I will give you a small introduction to the programming language Kotlin (which last year was added as a supported language for Android) and describe a few of my favorite features of the language.
Last year at Google I/O, it was announced that Kotlin would be added as supported language in Android. A lot of tutorials, introductions and examples has emerged from the community since, and it is clear from the Android Developer page for Kotlin that Google themselves want developers to see the advantages of Kotlin with a lot of helpful documentation and examples.
Android is growing rapidly in the automotive industry, among others Volvo and Audi has decided to use the Android operating system in their infotainment systems. This gives for a perfect reason to get some more knowledge about this rising star of programming languages!
In Android, Kotlin is compiled to JVM byte code so it is possible to call Java code from Kotlin and vice versa, which makes it easy to have a gradual transition from Java to Kotlin in an application. Kotlin is supported from Android Studio 3.0, which even introduced a feature to convert a file containing Java code to Kotlin. In my experience this function doesn’t work optimally but it gives you a head start on your way to update your application to Kotlin.
In Kotlin, the “billion dollar mistake” has been solved by forcing the programmer to express nullability explicitly in the type of a variable. By appending a question mark to the type, we let the compiler (and other programmers) know that the variable is nullable.
When we try to assign null to a variable of non-nullable type:
After appending the question mark, it is possible for the variable to hold null:
Safe call operator
In order to call a member function on a variable of nullable type, the safe call operator (?.) is one of the possible ways. If we try to call a member function the usual way, we get an error:
Though if we use the safe call operator, we get a different result:
As you can see, the safe call operator returns null if the variable it is applied to holds a null reference. The returned value will also be a nullable, in this case an Int?.
Another handy feature of Kotlin is the so-called Elvis operator (?:). It returns the value to the left of the operator if it is not null, and the value to the right if it is.
Continued reading about Kotlin null safety
For more information regarding null safety in Kotlin, check out the official documentation on the subject.
Mutability of variables
As you might have noticed in the examples above, a variable can be declared with the keyword var.
This creates a variable that is mutable, e.g. it is possible to alter its value at a later point. It is also possible to make a variable immutable, this is done using the keyword val when declaring the variable instead:
In Kotlin, it is possible to extend classes with your own functions without creating subclasses. This concept is called extension functions and they are declared like this:
In this example the standard class Int is extended with a function square that can be called on integers in all files the declaration of the extension is included in. See the official documentation for more examples.
What in Java is called a POJO, has its Kotlin counterpart in data classes. A data class contains a lot of extra functionality which would be boilerplate in a POJO, e.g. equals, hashCode and toString. This allows you to create a class with just a one-liner:
It is possible to declare a variable with either var or val in a class, with the difference that a var makes the compiler to generate a setter function for the variable in the class beside the getter that is generated for public variables.
The null safety of Kotlin is a big selling point of Kotlin and something that often is pointed out as an advantage of using Kotlin over Java when developing for Android (beside for verbosity). In this post I have scarcely scratched the surface of Kotlin and if you are interested in learning more I would recommend you to continue your reading on the official webpage of the language, but also on the Android Developer page for Kotlin.
// Johannes Jansson, Software Developer