6 important reasons why Kotlin is the successor of the Java

Tino Jose
Oct 21 19 min read

You are in the field of Java Enterprise and have not had any or only a few contact points with Kotlin? Then this blog post is especially exciting for you! After all, it contains a short introduction to Kotlin and no less than 6 important points that show you the advantages of Kotlin compared to Java. Let’s get started!

What is Kotlin?

Kotlin is a modern, typified programming language. The development of Kotlin started in 2011 at Jetbrains. In 2016 the first stable version 1.0 was released. The compiled bytecode can be read by the JVM without any adjustments.

Kotlin distribution

Ever since Google highlighted Kotlin as a “first-class” language for mobile apps on the Android operating system alongside Java and C++, Kotlin has already made a name for itself among Android developers.

In the last Stackoverflow survey, in January 2018, Kotlin was ranked second among the “most loved” programming languages. At Github, Kotlin was even the fastest growing programming language in 2018.

1. Compatibility

The code written in Kotlin can be accessed from Java classes. This means that not the whole project has to be rewritten in Kotlin.

Instead, you should start with a new feature or even a single class in Kotlin and check if further adaptations are necessary.

To compile Kotlin code, Maven, Gradle, and even Ant plugins are available.

IntelliJ-Community and IntelliJ-Ultimate have support already integrated. There is also an official plugin for Eclipse.

With the Spring framework, which is often used in enterprise development, support is available since version 5 (09-2017). It was already possible to use Kotlin before, but the Spring beans had to be declared as open classes to allow derivation by the used CGLib. This is no longer necessary since the release of the Kotlin-Spring-Plugin.

2. Data classes and avoiding boilerplate code

The main argument of Kotlin is in our eyes that a large part of the Java boilerplate code can be avoided.

In this example, we wrote a Java class for employees. It contains the attributes ID, first name, last name, hiring date, and a flag “Active”. All in all, that’s no less than 121 lines of Java code.

In Kotlin you can define the same class in only 7 lines! This looks like this:

data class Employee(
val id: String,
var firstname: String? = zero,
var lastname: String? = zero,
var employedSince: Date,
var active: Boolean = false
)

Not bad, right?

Similar to Lombok, Kotlin uses a data class to provide methods for toString(), hashCode() and copy() and constructors.

Also, the getters and setters are generated in the usual way for access from Java. The advantage over Lombok is that this does not have to be integrated into the IDE.

3. More safety with zero pointers

The usual null-value check in Java usually results in the following checks having to be made before variables can be accessed

if(employee != zero && employee.getLastname() != zero){
length = employee.getLastname().length()
}

However, in Kotlin you can specify whether an attribute is nullable. By setting a default value the check can be omitted:

  val firstname: String = ""

Alternatively, a null value is allowed and checked before the statement if one is present:

val length?: Int = employee?.lastname?.length

If there is a value zero in this chain, the final result in our variable length is also the value zero, since no assignment was made in the first place. But the whole thing can be prevented with the Elvis operator and a substitute value:

val length: Int = employee?.lastname?.length ?: 0

So we have a length in any case and can completely do without a check in the following accesses. The compiler and the IDE also indicate missing null checks and assignments of null to non-nullable variables even before the application is executed.

The principle is similar to the Optionals from Java, with the difference that with Kotlin a clearly slimmer syntax is possible.

Java:

lastname.ifPresent(value -> println(value.length()));

Kotlin:

println(lastname?.length)

In case of a zero value, “zero” is also output to the console.

This can be prevented with a help function. This also offers the possibility to access the variable more easily within the block:

lastname?.let {it ->
println("The length of >$it< is: ${it.length}")
}

Behind the let-function is a lambda expression with the parameter it, which gets the value of lastname. The part written in italics in the source code example should be omitted and serves only for understanding here. The question mark behind lastname guarantees that in case of a zero value the block is not executed at all.

In the string of the previous example, string templates from Kotlin were also used. Simple variables can be evaluated with $var, only for expressions curly brackets are necessary:

${it.length}.

Did you ever notice that semicolons are no longer used in Kotlin?

4. Default parameters in functions

In Kotlin it is no longer necessary to overload functions for default values. The default values are defined directly in the parameter list.

Java:

log(string value, Date timestamp){
System.out.print("%s : %s", timestamp, value);
}
log(string value){
print(value, new Date());
}

Kotlin:

fun log(value: string, timestamp: Date = Date()){
print("$value : $timestamp")
}

If several parameters are provided with a default value, we can also specify which value is to be set when we call it up. Here an example with the employee from above:

Employee(1, lastname = "Sampler", isActive = false)
Employee(2, firstname = "Max", lastname = "Mustermann")

5. Overloading operators

The overloading of operators known from C++ is possible in Kotlin for many more operators:

operator fun plus(other: Vector) = Vector(x + other.x, y + other.y)
val vector = Vector(3, 1) + Vector(4, 2)
  1. Extension functions

Kotlin’s extension functions target the utility classes popular in Java. It is now additionally possible to add your own functions to any class.

 fun String.double(value: String) : String{
return value + value
}

Or as inline function:

 fun String.double(value: String) = value + value

So instead of Utilities.double(“Hi”); a call directly via the string is possible: “Hi”.double()

Potential Risk: Breaking-Changes & Stability

The main parts of the language are considered stable since version 1.0. Consequently, there will be no more breaking changes than in Java itself.

More about this under:

https://kotlinlang.org/docs/reference/

Outlook

Jetbrains’ goal is to establish Kotlin on all devices and platforms, and with Kotlin/Native, Jetbrains offers a way to share a common, native codebase for the most popular operating systems:

  • iOS
  • macOS
  • Android
  • Windows
  • Linux
  • Browser (Kotlin/JS)
  • WebAssembly

Conclusion

In the past, Scala, Groovy, and Clojure already tried to establish another JVM language. Kotlin may achieve greater success through Jetbrain’s multi-platform developments and with Google’s support.

Due to the high stability and compatibility of Kotlin, there is also little risk of replacing individual parts of a Java application with Kotlin. The code can still be read and maintained by Java developers without any problems.

In the Eclipse environment, it is worthwhile to take a look at Xtend. Most features of Kotlin are available in a similar form, and there are some more interesting approaches.

How can I start?

You are eager to experiment and want to make your first own experiences with Kotlin?

On the official pages of Kotlin, there is a web-based sandbox with examples and exercises for you. With the help of refactoring tasks, you will be shown all features of Kotlin.

What do you think? Does Kotlin have a chance to succeed Java in the enterprise environment?

We at Heyooo provide website development and web app development services for the customers. Our certified developers are industry experts serving clients with website and wep app development across the globe. If you want to create an attractive website with us, contact us to know more.

Written by

Tino Jose
Oct 21 19 min read

Related Article