meshBlog
Our Top 5 instant benefits of the Java to Kotlin transition
I started working at meshcloud about one month ago and from the first day on, I was writing Kotlin code without ever having seen one line of Kotlin before. The most amazing thing about Kotlin is how quick and seamless you can shift from Java to Kotlin. There was not a single day on which I was lost with the new language. Of course, I currently only touch a small part of what Kotlin can do. But these few percent are already such a tremendous gain in productivity and come with almost no learning curve at all.
What is it that makes me talk about Kotlin so overwhelmingly? I want to explain it with our Top 5 advantages you can directly benefit from when switching from Java to Kotlin.
1. Null Safety
The biggest advantage of Kotlin is Null Safety in my opinion. It allows you to get rid of all those ugly null checks you have to write in Java. The time of NullPointerExceptions (NPEs) belongs to the past with Kotlin. Variables are not nullable by default in Kotlin. If you need a nullable variable, you can define it with an additional “?”:
val myText: String?
If you now try to access this variable without a null check, you get a compile error. That way NPEs cannot occur during runtime. Kotlin also provides further support in using null checks. You can write something like the following:
val length = myText?.length
If my Text is null, length will also be null. To find out some more details on Null Safety, please have a look at the reference documentation.
2. Implicit Getters and Setters
You will never have to write default getters and setters in Kotlin again. They are available by default for all class variables. You can even access those variables directly and don’t have to use the explicit getter and setter calls:
class Person {
val firstName: String
val lastName: String
}
// access class Variables
val person = Person()
val firstName = person.firstName
Even though accessing those variables directly, overwritten getters and setters are still applied:
// extend Person class with a setter
fun setFirstName(firstName: String) {
this.firstName = "Kotlin"
}
// set firstName
person.firstName = "Brian"
println(person.firstName) // prints: Kotlin
3. Data Classes
Data classes take the default getters and setters approach and extend it further. You also get an implementation of equals
, hashcode
, copy
and toString
methods based on the class variables.
data class Person(val firstName: String, val lastName: String)
That’s all it takes to define a class with all the functionality you usually generated with your IDE in Java. As it was generated anyways, you can save those tens or hundreds of lines of code when using Kotlin. And of course, you can still override the default implementation.
4. String Templates
Kotlin provides a simple syntax to build Strings. In Java you usually either used string concatenation, string builders or String.format. All of these options have their drawbacks, especially regarding readability. With String templates in Kotlin, you simply write a text with references to variables like this:
val text = "Hello ${person.firstName} ${person.lastName}!"
You can directly see how this string will look like. In Java, you would most likely use String.format for that, which is harder to read:
String text = String.format("Hello %s %s!", person.firstName, person.lastName);
5. When Expressions
This is something you perhaps don’t need every day, but I always felt a bit awkward when writing switch-case statements or longer if-then-else cascades. Of course, you should avoid them in the first place but sometimes, e.g. for different handling of enum values, you need them. Kotlin provides a really nice and clean solution for this:
when {
"Wayne" in lastNames -> println("Hello Batman!")
"Kent" in lastNames -> println("Hi Superman!")
lastNames is empty -> println("Nobody’s here")
else -> println("Hello!")
}
When statements are also a lot more powerful than a switch-case statement. The example I used, checks on lists to show this. You can even check different objects in a when condition.
How to profit from Kotlin yourself
Integrating Kotlin into your existing Java application is just as easy as it is for a Java developer to read basic Kotlin code. I.e. when using Maven, you only have to add the Kotlin Maven Plugin.
Now you can start writing Kotlin files which can also be used from within Java classes and vice versa.
At meshcloud we started with a plain Java Spring Boot application and are now converting it step by step into Kotlin. We write new classes in Kotlin and convert existing classes step by step as we touch them. The best thing about converting existing classes is that IntelliJ IDEA provides an automatic conversion from Java to Kotlin. That way you don’t even have to care about the basic conversion of existing Java files. Even when you copy a Java snippet to your Kotlin class, the IDE will ask you, whether you want to convert this snippet to Kotlin.
Conclusion
Kotlin is often described as the better Java and that is exactly what we experienced here at meshcloud. You get rid of lots of boilerplate code you have to write in Java. We recommend every Java developer to have a look at Kotlin. There is almost no risk in trying out Kotlin due to the very low learning curve and the seamless integration into your existing Java application. We would be glad to hear about your experiences in switching to Kotlin in the comments section. We hope you enjoy it just as much as we do.