Kotlin advantages over Java

Antonio B.  June 7, 2019


When you think about Android, Java is probably the first language that comes to your mind. From the beginnings, Java has been an official development language of native Android applications. However, Android was long in need of a modern, more robust and less verbose language. Kotlin was imposed as the best choice. Back in 2011. Kotlin was created by JetBrains out of their practical needs. They had most of their products built in Java and then started to realize its shortcomings. The need for a new language has become bigger but JetBrains had many applications written in Java and it just wasn’t practical to rewrite all their applications into a new language. They came to the idea to write language that is interoperable with Java so that they could add new features to old applications using the new language. That is how Kotlin was made.

On the Google I/O 2017 Android team announced new official development language, Kotlin. Its popularity has suddenly increased and many professional Android developers started to use Kotlin to develop their apps. Two years later Google announced another big news that Kotlin became their preferred language. In this article, I will introduce you to the basics of Kotlins conciseness, simplicity and flexibility over Java.

Data class

The best example where you can see conciseness between Kotlin and Java is the data class. In the code below you can see that with Kotlin you have to write much less code and code is cleaner than in Java just by putting one simple keyword called data before the class name. Kotlin took care of all the getters, setters, equals, toString and other functions for you. Having that in mind, writing more code means you are more prone to errors which is, obviously, something that we don’t want.

java vs kotlin

No more findViewByIds

Another great example where you can see Kotlin conciseness in action is using Kotlin Extensions plugin. Using findViewById is one of the most common operations in Android and it becomes boring and repetitive to write it all over again. Kotlin Extensions will allow you to recover views from activities and fragments by generating some extra code. Views can be accessed with the name of the id you used in layout definition. The plugin also builds a local view cache. When a property is used for the first time it will do a regular findViewById and every next use in order to improve access speed, the view will be recovered from the cache.

java vs kotlin

Type inference and expressions

val name: String = “John”

val name = “John”

As you can see from the first example in order to declare a variable you have to specify its type. This is how it works in Java, but in Kotlin you can choose whether you want to specify variable type or not. If you use second approach compiler will implicitly infer variable using type inference.

Kotlin offers you two choices of keywords to declare a variable. These keywords are var and val. Var is more like general variable and it is known as mutable variable which can be reassigned. Val is like final variable in Java and it is known as immutable variable which can be initialized only once. It is always recommended to use val on all variables when possible. That way you make your code safer for multithreading, because it ensures your variables cannot be modified by other threads unexpectedly. Another great thing about val keyword is that you can declare it with a type only and assign it a value later. This comes handy when using expressions which we will see in the next examples.

Kotlin is supporting both object oriented and functional style of programming. This allows you to assign different expressions to variables. For instance, you can assign whole if/else block to a single variable.

val isAdult = if (person.age >= 18) true else false or shorter val isAdult = person.age >= 18

At least once you have been in situation where you have to get result from try/catch block. Scenario in Java goes like this: declare variable first, initialize it inside try/catch block and then you are not sure whether it is initialized or is it null. Using Kotlin, you can simply assign whole try/catch block to the variable itself and in case it is null you can use power of Kotlins nullability (will be explained later in this article).

java vs kotlin

I won’t cover all the expressions in this article but one that I like most is when statement which is advanced form of Javas switch. On the example shown below you can see Kotlins when compared to Javas switch. When can be used either as an expression or as a statement. Switch can be used just as a statement that can substitute a series of simple if/else that make basic checks while when has better and safer design, can have arbitrary conditions expressions and can be used without an argument.

java vs kotlin

Null safety

Person person = null // Java

val person: Person? = null // Kotlin

In Java, you are able to assign a null value to any variable which causes NPEs if they are not handled before whereas in Kotlin, by default, all types of variables are non-nullable which means that you are unable to assign null. If you try to do that code will fail during compile time. In order to assign null to any variable, you have to use ? operator to tell the compiler that you will take care of nullability. In case you set variable to be nullable you are then responsible to take care of NPEs that may happen in case the variable becomes null. Kotlin gives you more choices to handle nullable variables than Java. For instance, you can reassign variable to be non-null using !! operator or you can keep it nullable and use ?. operator that means if a variable is null do nothing or in other case do the work that should be done. Keep in mind that using !! operator is not safe and use it only if you are compelled to. To make it clearer look at the examples below.

java vs kotlin

Of course, there are more ways to be used in Kotlin in order to handle nullable variables. One of them is let extension function. Let takes the current value of a variable and forwards it into a lambda block if a value is not null. In case the variable is null nothing happens. As you may guess let function acts same as Javas “if not null” block.

java vs kotlin

Concluding Kotlin vs Java

There is plenty of more advantages that I didn’t cover but this should be enough to surely say that Kotlin is better than Java if you use it to develop Android applications. We can’t say that Kotlin is a replacement for Java but we can say that Kotlin is enhanced version of Java. However, Java built its popularity for over 20 years and many projects are still written in Java so it is hard to say that everyone will switch to Kotlin in the near future. As developers, we always have to be aware of the latest trends, whether we decide to follow them or not. In order to stay relevant and to write more elegant code, I encourage you to start learning Kotlin, you won’t regret.

arrow white

Previous article

Cinnamon Agency Recognized as a Top Creative and Design Agency in Croatia by Clutch!

Get in touch

Tell us about your project, ask us any question or simply say hello.