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.
Kotlin is more secure than Java, mainly due to two language features
Null references have been called “the billion dollar mistake”. While this term has been coined in other languages, every Java developer can tell a story about hours of hunting those dreaded NullPointerExceptions…..
The following code does not compile:
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?
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
By distinguishing between *val*ues and *var*iables, Kotlin makes a clear distinction between mutable and constant (immutable) values. Java has the *final* operator, the main problem is that the developer has to write an additional keyword to create an immutable value.
2. Speed of Innovation
Java has gone through a major ramp up regarding speed of innovation. Currently Java is releasing a new major version of the Java language every 6 months.
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:
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:
Kotlin also has a clear strategy of becoming a ubiquitous language, running both in the browser and natively on systems where no JVM is available or a VM is generally not the preferred way to run programs.
Integration with Java
Kotlin integrates very well 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
- 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.
Kotlin Native is the initiative to create a version of Kotlin runnable on all “important” platforms like iOs, Android, and also WebAssembly. This might be a game changer…
Wait, you said “no downside to java”, this sounds like a sales pitch
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.
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…
Having said this: I am sure that the benefits outweigh the drawbacks big time when using Kotlin instead of Java. You have to introduce this new language, teach the “right” way to do stuff in Kotlin, coach the developers and continuously improve the patterns and techniques used.
Innovation is taking place at a breathtaking pace, while still managing to be simple enough to be the “first language” to learn.