Why Kotlin

Christian Sprecher
May 13, 2019 · 4 min read
Image for post
Image for post
Photo by Pen Tsai on Unsplash

Kotlin is a rather new language, developed by JetBrains Inc., which runs on the JVM, in the browser and natively on Platforms like Android and iOS.

Here are the three most important reasons why I think that Kotlin is THE language to use on the JVM.

Image for post
Image for post
The beauty of Kotlin

1. Security

Null safety

The following code does not compile:

Null safety — compile exception

But if you want to do something with those nullable values, you have to check for null: (Using the ?. operator. This is the "null safe dot operator", or "safe call operator").

→ Before running the snippet further down: what do you think is displayed?

Null safe access

While not displaying something meaningful (i.e. “null”) it at least does not crash.

The syntax of Kotlin is also way more succinct than the syntax of Java: a question mark is both simpler to use and more intuitive than Javas “Optional”.

See https://kotlinlang.org/docs/reference/null-safety.html for more examples

Emphasis on Immutability

Kotlin has one main construct to prevent shared mutable state:

val vs var keyword

Reassignment to a value: not possible

2. Speed of Innovation

This is good news for all Java developers, but there is one problem: most businesses are not able to keep up with that speed, and therefore most businesses are still on JVM 8 (or even older versions).

Kotlin has its own release cycle, and is happily running on older JVM versions. This allows for innovations like coroutines:

Asynchronous execution

Without going into details, coroutines allow to declare a function as beeing “suspendable”, which makes it possible to interrupt its execution and let it continue at a later point in time. This facilitates a transparent asynchronous execution with as little overhead as possible both for the programmer and for the runtime (no that many threads where harmed in the execution of this program…).

Coroutines alone might be a killer argument for Kotlin. But it doesn’t stop there, Kotlin has other advanced features. One of the most interesting ones are smart casts.

Try to initialize the var x once as String and once as Int. The Kotlin compiler is intelligent enough to cast to the respective type:

Smart Casts

3. Integrations

Integration with Java

  • Within IntelliJ, you can choose to convert one Java class after the other (doesn’t work too well, although)
  • The only requirement to have Kotlin ready is a 1.8 JVM
  • Integration with Maven is seamless, if you prefer it over Gradle

Integration with JavaScript

  • Kotlin could be used to define objects which are created and consumed both in the browser and on the backend.
  • Kotlin can make use of the ts2kt TypeScript-> Kotlin converter to create Kotlin artifacts out of Typescript definitions.

Native Integration

Wait, you said “no downside to java”, this sounds like a sales pitch

Image for post
Image for post
Source: https://thedailysales.net/the-10-best-sales-memes-ever/

Ok, let me elaborate: using Kotlin is overhead, plain and simple: there is another compiler (arguably slower than java’s), there is the overhead to learn a new language, and kotlin itself will never be the “first” language on the JVM.

More about the hidden costs of Kotlin on this wonderfull post on Medium.

And then there is always the possibilty to shoot oneself in the foot, with style, i.e. by using something idiomatic but having a performance overhead.

from https://craigrussell.io/2019/03/shooting-yourself-in-the-foot-while-adding-an-element-to-a-kotlin-lis

One also has to consider the fact that Kotlin is being developed by one company (Jetbrains), which is imho one of the greatest product company and has a fair and balanced approach towards the developer community (Kotlin itself is open source). While one can compare Oracle to Jetbrains, one thing is sure: Oracle won’t be bought up in the near future…

Conclusion

Kotlin, in a way, is more comparable to languages like typescript or maybe even c#.

Innovation is taking place at a breathtaking pace, while still managing to be simple enough to be the “first language” to learn.

If you want to try out Kotlin: Just go ahead and play with it, you won’t be disappointed…

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store