Why you should use Kotlin instead of JAVA in your next project

Kotlin is an open-source statically typed programming language designed by JetBrains. It is object-oriented and supports functional programming features.

When you hear Kotlin, you will probably think about it as a programming language merely for developing mobile apps on Android, and you are right – but only partially. That is because Google decided that Kotlin was its preferred language for building Android apps. Yet this language is much more than that, Android being only a fraction of possibilities Kotlin offers. But let’s start from the beginning…

What is Kotlin?

It is an open-source statically typed programming language designed by JetBrains. Yep, the same guys who created IntelliJ IDEA. You may say “IntelliJ is the best IDE for JAVA, those people know how to do the stuff, this Kotlin might be a really good tool” – and you are right! I mean, if you like JAVA and think it is a cool language then Kotlin is even cooler. Why? I will show you later.

Kotlin is object-oriented and supports functional programming features. It is designed for the JVM (Java Virtual Machine). What does that mean? That is fully compatible with JAVA. Actually, to be honest, Kotlin is Java with some extra pieces of stuff. What does that mean? Those languages can be used in parallel in the same project. For example, you can easily use JAVA libraries in Kotlin’s project, but Kotlin gives you more out of the box. It is focused on interoperability, safety, clarity, and tooling support. You can use it to a code Back-end server, Andriod Apps or even iOS app using Kotlin Native. Another interesting fact is that you can compile Kotlin to JavaScript.

OK, that sounds good, but why is it better than JAVA?

The first argument which comes to my mind is Kotlin’s syntax. When working with Kotlin, we can reduce the code by a lot of lines. Such code will be more transparent and easy to understand. JAVA is a really nice language, but as the project expands, the number of code lines increases logarithmically. Eventually, you will need a significant amount of time to find what you are looking for in your code. Developers of Kotlin tried to get rid of this disadvantage, and they did so very well. I will not show you every difference in syntax because it would take much more space than a blog article ever should, but I can explain the basic idea in simple cases. How many DTO objects have you written? Every one of them looks similar to getters, setters, hashCode, equals, toString, and constructors? For simple objects like for example Student with his id firstName, surname, age, email, country it might be over 50 code lines. How this object will look in Kotlin?

Yep, that is right, just one line. This line will compile full JAVA objects which will have getters, setters, hashCode, equals, toString, and constructors. As you see, the syntax is a little bit different and you may wonder why the country has default value = “”, it is for the purpose though. Because of that, in this case, we have a constructor with all arguments and constructor with all arguments without a country. If it is so easy, imagine how many code lines you would have saved. Of course, that is a really simple case, only to show you the difference. You will probably ask what if I don’t define my own getter or setter? Or maybe overwrite toString or even write its own method? It is possible in Kotlin and be sure that it is more transparent than in Java.

What if I told you, never again of Null Pointer Exception?

Do I have your attention now? I hope so. How many of Null Pointer Exceptions have you seen in your life? Especially at the beginning of your journey with JAVA… In Kotlin you will probably never see NPE again. Because Kotlin has null safety implemented through the introduction of the nullable and non-nullable type separation. It is by default disallow null types. But sometimes you want some variable to be null and that is easy to achieve. Do you have to add char? at the end of the declaration of the type. For example var name: String? that means variable name can be nullable. But if you will not check null safety, Kotlin would not let you compile your code. Of course, it is still JAVA so NPE is possible, but only if this is what you truly want. You can use !! operator, if you do so and don’t check null safety, NPE will be thrown. But this will not happen unless you desire it to.

And what does it mean? A higher-order function is a function that can take a parameter of a function or returns a function. Kotlin supports inline functions. So when you have a simple method you can code it in just one line, for example:

Another thing is that functions can be stored in variables and data structures. And what else? Kotlin is improving JAVA’s lambdas. Lambda syntax looks like this:

In lambdas, the only code body is not optional. And of course, we don’t need to set it to a variable, we can pass it directly as a method argument. For example, the addition will look like this in lambdas:

Smart type casting

This is not a game-changer, but still a pretty nice enhancement. Kotlin compiler is tracking code logic and automatically cast type object if it is possible, for example:

Easy migration JAVA to Kotlin

This is actually a great advantage of this language. As I said before, Kotlin is fully compatible with JAVA. Even better – they can both coexist in the same project. What does it mean? That you are free to migrate your existing JAVA project to Kotlin in small steps, class by class, method by method.

Really good documentation and community

This may be the most valuable, yet not obvious benefit, but helpful and thoughtfully crafted documentation is important. Especially when you get more experienced, you start to appreciate well-written documentation. In Kotlin you will be surprised by its high quality. Also, there is a fast-growing community that can help you with a lot of issues you may encounter. At this moment it is worth to point out known players who work with Kotlin on daily bases:

Google.

Amazon.

Netflix.

Pinterest.

Uber.

Trello.

Kotlin and IntelliJ

As I said before, Kotlin was designed by JetBrains, the same people who designed Intellij. I think that is obvious that IntelliJ works really well with Kotlin. You can easily start a new project with your IntelliJ, you don’t have to install any extensions. IntelliJ gives you many tips and hints while coding in that language, but what is most important, it gives you the possibility to automatically convert JAVA code into Kotlin. Of course, it is not perfect, and you would probably still come up with better solutions using your own brain, but it works well enough.

Kotlin and Spring sound like a great couple

Kotlin supports Spring and Spring boot framework, so you can use the most popular framework with Kotlin and benefit from the advantages of both of them.

Summary

In this article, I mentioned only a few most distinguishable areas that speak for using Kotlin in your project. There is more, and definitely too many to cover them all here. You can find more information on the web and I strongly recommend it to you. If you decide to try Kotlin, you wouldn’t regret it. Actually, there is a great chance that you wouldn’t want to work with JAVA anymore. It is not like you have to leave JAVA and work with Kotlin because it is the best. It is because coding with Kotlin is just nicer, more enjoyable and can make you like your job even more.

Of course, Kotlin is not perfect and it has some detriments. For example, it is not the best programming language for beginners. Why? If you don’t have experience with JAVA or similar languages it might be confusing. Some things happen automatically and it can be hard to understand what is going on. In my opinion, fully understanding JAVA before learning Kotlin is the optimal path here.