#57: Kotlin: Much more than "better Java"
Kotlin is a programming language that runs mainly on Java Virtual Machine. This means it’s fully interoperable with Java and even other JVM languages. Developers can gradually rewrite their applications from Java to Kotlin. Or use Java libraries and frameworks inside Kotlin. But why bother with a new language? Kotlin has plenty of improvements over good old Java. Sometimes it’s placed between Java and Scala in terms of capabilities. It seems more modern, agile, and powerful.
The most striking feature is null safety.
In Java, every variable can be null
, with a few exceptions.
In Kotlin, by default, variables can’t be null
.
If you need some notion of optionality, simply add a question mark next to the type.
Moreover, you can’t simply call methods on nullable types.
You need to check first null
first.
This effectively prevents dreadful NullPointerException
.
Another common source of bugs in Java is mutability. Kotlin promotes immutable variables and data structures. This is especially useful with another language feature called data classes. Data classes are simple data holders with a name and a bunch of attributes. This concept replaces dozens of lines of generated code with a compact one-liner. As a matter of fact, data classes were added to Java as well, recently. Under the name records.
Coroutines
But all that pales in comparison to Kotlin’s killer feature: coroutines. Coroutines allow writing very readable, asynchronous code. No more reactive frameworks, futures and promises. Code looks like it was imperative and running sequentially. Under the hood, each method that blocks is suspended. It means another thread can take over, utilizing resources very efficiently. Once again, a similar feature is scheduled for Java, one day. Under the name Project Loom.
Other platforms
I said that Kotlin runs mainly on the JVM, as a better Java. This is no longer true. First of all, it’s the language of choice for Android developers. But that’s just the beginning. Kotlin can be compiled into JavaScript. This allows writing frontend applications in the same language as the backend. Like with Node.js, but using a decent language and runtime.
Also, Kotlin can run natively without JVM. In that case, it’s self-contained. Applications written in Kotlin Native can be distributed independently.
Adoption
The industry adoption of Kotlin is quite impressive. It was initially developed by JetBrains, a company behind popular programming IDEs. After being open-sourced, it’s popularity grew steadily. Especially after Google announced it’s the official language for Android. A ton of popular mobile apps, as well as backend servers utilize Kotlin. Chances are you are listening to this on a podcast player written in Android?
On the other hand, more conservative companies look at the development of Java more closely. Some people see Kotlin as a playground for new language features. If they work well, they may be added to newer versions of Java.
That’s it, thanks for listening, bye!