Migrating from Java to Kotlin

By | 14/04/2020

What is Kotlin?

It’s a programming language that runs over Java VM. Originally used to develop Android apps, but lately it’s been gaining ground in back-end development

Isn’t Java enough for you?

Java is an excellent programming language. But it was born in mid 90’s and it’s been a long while since that. Software development evolved, and although Java accompanied this evolution, it was left behind comparing to several new languages, who have more advanced features in a more natural way, as they where already conceived with those features from scratch.

Kotlin main features

Data Class

It’s Java’s main absence. When we just need classes to acummulate info, transfer between layers without logic, data classes are our allies. They provide us with copy and equals methods, and automatic implementation of setters and getters. You are going to save a lot of code just by using data classes.

Null safety

Kotlin data types don’t allow null values, unless we say the contrary. Does it mean that I don’t have to nullcheck everything? Exactly. So, how do I get nullable value types when I need them? I have to indicate that to kotlin when I declare variable types. This has 2 advantages: The first one is that for non nullable values, I don’t have to perform nullchecks. The second one, is in case I have nullable values, I MUST perform nullcheck, otherwise, compiler will scream at me, so I won’t forget to do it. And when I do it, I can use “let” and “run” instead of “if (value != null)”, which are more readable in the end.

High Order Functions

Several times I wanted to send functions as parameters to a method to get a simpler design, but in Java you have to involve functional interfaces and that’s not the friendliest way to do it. In kotlin this concept of functional programming is provided easily to enhance our code.

Interaction with Java code

Kotlin was born to be used in conjunction with Java. I can have a Kotlin class in a project nearly entirely made with Java, I can invoke it from and to Java classes, and everything keeps working as a charm. Being so easy to integrate with existing Java code enables us to try it and easily rollback in case we don’t like it (which is highly unlikely)

Low learning curve

Kotlin was born for Java developers who wanted to have some extra features and a more friendly code. It’s not a start from scratch. Any Java developer can benefit from Kotlin in a few hours.

Collections API

Map, filter, forEach, first, last and others are factory included. You don’t have to write “myList.stream().map(…).filter(…).collect(Collectors.toList())” In Kotlin is as simple as myList.map(…).filter(…)

Extension functions

Have you ever thought “It would be nice if this class had this method”and you couldn’t add it because it was from a library, or wasn’t under your scope? With Kotlin we can declare extension functions to a type, and we can invoke it everywhere.

How to try Kotlin?

If you use Maven for your builds, it’s as simple as copy pasting a plugin in your pom. I’m not going to expand over this, just read official docs from Kotlin itself: https://kotlinlang.org/docs/reference/using-maven.html

A very important thing to keep in mind is to have an IDE with good language Integration. As kotlin was developed by Intellij, language support in this ide is excellent, Kotlin also has eclipse plugins, but I haven’t tried it yet.

Final thoughts

If you are coding with Java, and you have a couple of spare hours, give yourself the chance to try Kotlin. If you ever tried Scala, you wil find most of it’s main features, at a much lower price.

Satis-function guaranteed.

Leave a Reply

Your email address will not be published. Required fields are marked *