Cinnamon logo
Cinnamon logo
Cinnamon logo
Close

Home

Projects

Services

About Us

Careers

Blog

Let’s collaborate

Close

Key Differences Between Java and Kotlin

Antonio B.

2019-06-07

6min

Development

In this article, we’ll introduce you to the basics of conciseness, simplicity, and flexibility of Kotlin over Java. What would be your choice?

placeholderKotlinAdvantagesOverJava.jpg

Share this blog:

twitter logo
facebook logo
linkedin logo
link logo

Introduction

When you think about Android, Java is probably the first language that comes to your mind. From the beginning, 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 a 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 a 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 the 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.

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.

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 the 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 a general variable and it is known as a mutable variable which can be reassigned. Val is like a final variable in Java and it is known as an immutable variable that 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 in handy when using expressions which we will see in the next examples.

Kotlin is supporting both object-oriented and functional styles of programming. This allows you to assign different expressions to variables. For instance, you can assign a 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 a situation where you have to get a result from try/catch block. The scenario in Java goes like this: declare the 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 the whole try/catch block to the variable itself and in case it is null you can use the power of Kotlins nullability (will be explained later in this article).

I won’t cover all the expressions in this article but one that I like most is when statement which is an advanced form of Javas switch. In 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.

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 the ? operator to tell the compiler that you will take care of nullability. In case you set a 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 a 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 the !! operator is not safe and use it only if you are compelled to. To make it clearer look at the examples below.

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 the same as Javas “if not null” block.

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 an 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 it.

Share this blog:

twitter logo
facebook logo
linkedin logo
link logo

Subscribe to our newsletter

We send bi-weekly blogs on design, technology and business topics.

Similar blogs

Job application illustration

You could use our expertise?

Let's work together.