Chapter 2. Java to Kotlin Projects

What is the first step of the journey from pure Java to a mixed and then increasingly Kotlin codebase?

Strategy

The first time we, Nat and Duncan, introduced Kotlin to a Java codebase, we were members of a small team that included six developers, building a relatively greenfield project. We had already deployed some web applications with Kotlin, but our enterprise architects insisted that we write the new system in Java 8. This was shortly after Kotlin 1.0 had been released, but before Google announced that Kotlin was an official language for Android, so the architects were understandably wary about committing to a language with an uncertain future for a strategic system that they expected to be around for decades.

In Java, we leaned toward a functional approach, designing the core application domain model as immutable data types transformed by pipelines. However, we kept bumping into Java’s limitations: the verbosity required to implement immutable value types, the distinction between primitive and reference types, null references, and Streams lacking common higher-order functions. Meanwhile, we could see Kotlin being adopted at an ever-increasing rate across the industry and even within the company. When we saw Google’s announcement, we decided to start converting our Java to Kotlin.

Our judgment was that starting in the core domain model would give us the biggest bang for our buck. Kotlin’s data classes shrank the code significantly, in ...

Get Java to Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.