Better Java than Java: Kotlin 1.4 introduces new compilers for JVM and JavaScript

JetBrains works on improving language for cross-platform targets


JetBrains has released Kotlin 1.4, a significant update to the Google-preferred language for Android development.

Kotlin began as a language targeting the Java Virtual Machine (JVM) but although its core use is for Android development, it can target other platforms including native code.

Kotlin/Native uses LLVM and compiles for iOS, macOS, Android, Windows, Linux, and WebAssembly. Kotlin/JS cross-compiles to JavaScript to run in the browser or on a server with Node.js.

Redmonk's language rankings place it at 19, with analyst Stephen O'Grady noting how impressive it is that "Kotlin, one spot ahead of Rust this quarter, was fully twenty behind it five years ago at #68", but adding that its upward trajectory has now stalled. JetBrains claims more than 5.8 million Kotlin coders over the past year.

Version 1.4 is perhaps the most significant update since 1.3 in October 2018. The language now supports SAM (Single Abstract Method) Conversions, previously only supported when working with Java methods and interfaces, enabling more concise and efficient use of lambda expressions.

Another change is the ability to mix named and positional arguments, and there are other small tweaks. Type inference is improved so that it applies to more cases, enabling wider use of the var keyword.

Kotlin Multiplatform lets developers write shared code for a common core, and platform-specific code where needed

Kotlin Multiplatform lets developers write shared code for a common core, and platform-specific code where needed

The more interesting changes are to the compiler and tools. There is a new Kotlin compiler, which JetBrains promises "will unify all the supported platforms". This is a work in progress, but 1.4 introduces new JVM and JavaScript back-end compilers in alpha, and promises that they "will become the default once we stabilize them".

The key here is the use of the same IR (intermediate representation) for JVM, JavaScript, and native targets, which means that compiler features and bug fixes need only be implemented once, whereas currently there are three different compilers. There is also improved interoperability with Objective-C and Swift on Apple platforms.

Kotlin Multiplatform is a project, currently in alpha, to share code across multiple platforms by defining a core language and libraries called Common Kotlin, that compiles for all targets, and writing platform-specific code separately. A new feature in 1.4 is called hierarchical structure support, and lets you share code between a subset of platforms, such as between different varieties of iOS.

The Kotlin Standard Library has a bunch of new additions, including new functions for arrays and collections, and new functions for bit operations, including the "experimental" rotateLeft() and rotateRight(). There is also the first stable version of the Kotlin JSON serialization API.

Kotlin's popularity is based on being an easy transition for Java developers, but with a type system that eliminates most null reference exceptions, and many additional features including extension functions, properties, operator overloading, and more.

The language is well liked – the fourth "most loved" language, according to StackOVerflow's survey – but there is a recent development that may erode its market share.

A discussion on the Kotlin forums asks the question: Should I learn Kotlin or go with Flutter? Flutter is not a language, but rather a cross-platform framework whose language is Dart.

"I am a big fan of both Kotlin but also Flutter... in terms of multi-platform development I can say that there is no contest, Flutter wins hands down and I am going to lobby to switch our KMP [Kotlin Multiplatform] project to Flutter when we get an initial release out," said one developer.

This is in the context of cross-platform rather than Android development, but with Google's big investment in Flutter it would not be surprising to see some Android-only developers also heading in that direction.

That said, the advantage of Kotlin is that you can use it wherever you can use Java, including for server applications using Spring, Micronaut or JetBrains' own framework for web applications: Ktor. If Kotlin loses some mobile developers to Dart and Flutter, it may still gain by being a better Java than Java elsewhere. ®


Biting the hand that feeds IT © 1998–2020