chapter nine

9 Kotlin

 

This chapter covers

  • Why Kotlin?
  • Convenience and conciseness
  • Safety
  • Concurrency
  • Java Interoperability

9.1 Why Kotlin?

Kotlin is a language created by JetBrains, makers of the popular IntelliJ IDE. Announced publicly in 2011, Kotlin aims to fill language gaps they felt developing in Java, without the friction they saw with other existing JVM languages.

Kotlin was open-sourced the following year, and reached 1.0 - with guaranteed levels of support and maintenance from JetBrains - in 2016. Since then, it has gone on to become the recommended language for the Android platform and gathered a solid following in other JVM coding circles. It has also reached beyond the JVM, supporting JavaScript and native backends as well.

As alternate languages go, Kotlin provides many quality of life improvements over Java, while not radically changing the entire world. Its focus on convenience, safety, and solid interop has given it a great story for incremental usage in existing Java projects. With first-class support in their IntelliJ IDE, turning a file from Java to Kotlin is often just a click away.

It’s worth noting that some features originally only available in Kotlin have made their way back into Java. A great example of this is Kotlin script - Kotlin can run a source file directly, typically with the kts extension, without the developer asking to compile. If this sounds like Java 11’s single-file feature that we showed off in Chapter 1, you’re not wrong!

9.1.1 Installing

9.2 Convenience and conciseness

9.2.1 Starting with less

9.2.2 Variables

9.2.3 Equality

9.2.4 Functions

9.2.5 Collections

9.2.6 Express yourself

9.3 A different view of classes and objects

9.3.1 Data classes

9.4 Safety

9.4.1 Null safety

9.4.2 Smart casting

9.5 Concurrency

9.6 Java interoperability

9.7 Summary