14 Reasons Why You Should Migrate from Java to Kotlin

Ani Margaryan
SFL Newsroom
Published in
5 min readDec 3, 2019

I have been doing programming for about 8 years already, 6 of which were devoted to doing Java. However, I switched to Kotlin around a year ago. The project’s Back-end was written in Kotlin half a year ago before me joining it. It’s a medium-size project with APIs exposed for both mobile and web usage.

So, now I have around 230000 lines of source code (including unit and integration tests)

in Kotlin and just 88 lines in Java (one Mockito configuration class).

In this article, I am going to share 14 reasons why it’s a good idea to switch from Java to Kotlin. Note that all the examples are from the project I was doing during the last year. Unfortunately, I am not going to talk about Coroutines since we haven’t used them anywhere yet.

Back to the benefits of Kotlin:

1. Shorter code

We are writing less code, no getter/ setter, and other types of boilerplate code. We are enjoying this advantage without losing anything from the Java ecosystem. We are using the same development tools (IntelliJ), the same frameworks (Spring, Hibernate) and libs (Apache Commons, Javax mail/cache).

2. Safety

Kotlin comes with built-in nullable and not nullable types. They help to avoid unexpected NullPointerExceptions in your code. Yes, we have the alternative @NotNull annotations in Java, but we hardly ever use them. In Kotlin you can’t avoid using not-null types.

For example, in the code snippet below, we have a value of a nullable type and with a null value assigned, then it converts to Optional without causing any issue:

3. Named arguments and default values

In Kotlin, you can call method/constructor by specifying argument names and it’s really preventing from bugs whenever you have a big number of arguments. Also, having default values for arguments makes a developer’s life way easier:

4. Extension functions

Here is what we cannot do in Java. We need to add some behavior in class which we can’t extend. Kotlin comes with a solution to this problem through Extension functions. Here is our use case:

5. Data classes

This nice Kotlin feature helps us to have some main commonly used function as hashCode, equals, toString, copy generated automatically. It speeds up the work while writing DTOs.

Your whole class will only consist of this simple piece of code:

6. Inline functions

Kotlin introduces inline functions, which means that a function and its parameters will be expanded at call. Here is one example from Kotlin Collections class which we have used a lot in our project:

7. Object expressions

Object expressions are used when you want to create a replacement for a class with some modifications. They are a good alternative for Java’s anonymous classes due to the clearer syntax:

8. Companion objects

When you need to have some function and property connected with a class, not with an instance, you can use companion objects. It’s a replacement for Java’s static members and functions and is better because it’s grouped in one place and is better from the design perspective too:

9. Type aliases

You can use them for introducing a new shorter and more meaningful name for the existing types which better fits your use case:

10. New flow controls operators

There are two new operators which we are using quite intensively — when and range. When is a good replacement for switch and range for for operator.

11. Easy design pattern implementations

There are a lot of known ways to implement a singleton in Java, but especially when we need to deal with multithreading it’s not trivial. The solution offered by Kotlin is shorter and easier.

And you can also easily implement delegation by just using the class name to which you want to delegate, using by keyword and then overriding the function that you want to delegate.

12. Write multiple classes in one .kt file

Java’s limitation to have an independent class per file very often forces us to create static inner classes. In Kotlin it has a good solution — just have multiple independent classes in one file!

13. Don’t deal with checked exception

Kotlin removed checked exception and it made our life easier. It’s not forcing to handle exceptions which we don’t want to deal with.

14. High order functions

Kotlin is more of a mix of functional and OOP programming. Here we can easily store lambdas in variables, pass them to function and have as a return type.

P.S. I just want to mention that there are also problems coming from Kotlin. For example, Kotlin’s final classes can have problems with Spring and/or Hibernate proxies. However, while you can meet obstacles when using Kotlin, they are also pretty easy to overcome.

P.S.S. Also using Kotlin will help you avoid thousands of these ; … you don’t need them anymore at the end of each line in Kotlin. :D

--

--

Ani Margaryan
SFL Newsroom

Software engineer. Enthusiastic about JVM based backend development and DevOps(AWS). Avid gym goer :D