People use Kotlin

6 important reasons for Kotlin as the Java successor in the enterprise area

Are you working in the Java Enterprise area and haven't had any or a few points of contact with Kotlin? Then this blog post is particularly exciting for you! Finally, in addition to a brief introduction to Kotlin, it contains no less than 6 important points that show you the advantages of Kotlin over Java. Let's start!

What is Kotlin?

Kotlin is one modern, typed programming language. The development of Kotlin started in 2011 at Jetbrains. In 2016, the first stable version appeared with 1.0. The compiled byte code can be read by the JVM without any adjustments.

Among other things, it is advertised with

  • the sleek and modern syntax and
  • the Prevention of null pointer errors.

Kotlin spread

Since Google highlighted Kotlin as a "first-class" language for mobile apps on the Android operating system in addition to Java and C ++, Kotlin has already made a name for itself with 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

Code written in Kotlin can be addressed from Java classes. This means that the entire project does not have to be rewritten in Kotlin.

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

Around Kotlin Code to compile, are available for Maven, Gradle and even Ant plugins.

Support is already integrated into the IntelliJ Community and IntelliJ Ultimate. There is also an official plug-in for Eclipse.

The Spring framework, which is often used in enterprise development, has been supported since version 5 (09.2017). It was already possible to use Kotlin before, but the spring beans had to be declared as in order to enable the CGLib used to derive them. This is no longer necessary since the release of the Kotlin Spring plugin.

2. Data classes and avoidance of boilerplate code

The Kotlin's main argument is in my opinion that a large part of the Java boilerplate code can be avoided.

In this example I wrote a Java class for employees. It contains the attributes ID, first name, last name, setting date and an "Active" flag. All in all, that's a whopping 121 lines of Java code for me.

The same class can be found in Kotlin in just 7 lines define! It looks like this:

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

Not bad, right?

Similar to Lombok, Kotlin cares by using a data class (data class) about having methods for toString (), hashCode () and copy () as well as constructors are present.

In addition, the getters and setters are generated in the usual way for access from Java. The Advantage over Lombok however, is that this does not need to be built into the IDE.

3. More security with null pointers

The usual Null checking in Java usually has the consequence that the following must be checked before access to variables:

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

 

In Kotlin, however, you can specify whether an attribute nullable is. The check can be omitted by setting a standard value:

val firstname: String = ""

 

Alternatively, a null value is allowed and a check is made before the statement whether one is available:

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

 

Should be a value in this chain zero be, the bottom line is in our variable length also the value zero, since no assignment was made in the first place. The whole thing can be prevented with the Elvis operator and a substitute value:

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

 

So we always have a length and can completely dispense with a check in the subsequent accesses. The compiler and the IDE also indicate missing null checks and assignments of before the application is executedzero for non-nullable variables.

The principle is similar to that Optional from Java, with the difference that with Kotlin a significantly leaner syntax is possible.

  • Java: lastname.ifPresent (value -> println (value.length ()));
  • Kotlin: println (lastname? .Length)

 

In the case of a null value, "null" is also output to the console.

This can be prevented with an auxiliary function. This also offers the possibility of easier access to the variable within the block:

lastname? .let {it -> println ("The length of> $ it  

Behind the let-Function hides a lambda expression with the parameter itthat has the value of lastName receives. The part in italics in the source code example should be omitted and is only used here for understanding. The question mark behindlastName guarantees that in the case of a zero value the block will not be executed in the first place.

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

Have you actually noticed that Semicolons no longer needed in Kotlin become?

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 should be set when calling them. Here is an example with the employee from above:

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

5. Operator overloading

The operator overloading known from C ++ is possible in Kotlin for many other operators:

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

6. Extension functions

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

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

 

Or as an inline function:

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

 

Instead of calling it directly via the string, it is possible:

Possible risk: breaking changes & stability

The main components of the language have been considered stable since version 1.0. As a result, no more breaking changes will occur than in Java itself.

More on this at: https://kotlinlang.org/docs/reference/evolution/components-stability

outlook

Jetbrains aims to Kotlin on all devices and platforms to establish, and offers with Kotlin / Native a way to use a common, native code base for the most common operating systems:

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

Kotlin / Native is currently still in development. For this reason, it should not be used productively yet.

More on this at: https://kotlinlang.org/docs/reference/native-overview.html

Conclusion

In the past, attempts have already been made to establish another JVM language with Scala, Groovy and Clojure. It is quite possible that Kotlin will achieve greater success through Jetbrain's multi-platform developments and through Google's support.

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

In the Eclipse environment, it is worth taking a look at Xtend. Most of Kotlin's features are similarly available, and there are a few other interesting approaches.

How can I get started?

Are you keen to experiment and would like to gain your own experience with Kotlin?

There is one on the official Kotlin website web-based sandbox with examples and exercises to you. With the help of refactoring tasks, you will be shown all of Kotlin's features.

Have fun playing!

What do you think? Does Kotlin have a chance to succeed Java in the enterprise environment? I am pleased about your comment!