Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Kotlin 1.5 Gets Support for Java 15 Features and a New JVM Compiler

Kotlin 1.5 Gets Support for Java 15 Features and a New JVM Compiler

This item in japanese

Lire ce contenu en français

Kotlin 1.5 introduces support for the latest Java features, including record classes, sealed interfaces, and inline classes. Additionally, it brings a number of improvements to the standard library and a new JVM IR compiler.

Java records are classes that contain immutable data. A Kotlin data class can be used as a Java record by annotating it with @JvmRecord:

data class User(val name: String, val age: Int)

To be used as a record, a Kotlin data class must not derive from other classes, since all Java records derive from java.lang.Record, but can implement interfaces. Additional requirements forbid declaring mutable properties with backing fields and local classes.

Kotlin 1.5 also introduces support for sealed interfaces and relax requirements for sealed classes. Both sealed interfaces and classes are Java features aiming to provide fine-grained control on inheritance by enabling classes and interfaces to specify their allowed subtypes. Allowed subclasses of a sealed class are known at compile time and cannot be added thereafter. Sealed classes can be declared by prefixing them with the sealed modifier. In Kotlin 1.5, sealed interfaces and classes are not required anymore to belong to the same file and can appear in any files in the same compilation unit and package.

Finally, Kotlin 1.5 stabilizes inline classes, which are value types that do not incur memory allocation-related overhead. An inline class can be declared using the value modifier:

@JvmInline //required for the JVM backend
value class Password(val s: String)

As mentioned, Kotlin 1.5 also brings a number of improvements to the standard library. In particular, it introduces a new API for unsigned integers, idiomatic extension functions to use non-blocking Java I/O, and improvements to the String and Char API.

As part of a project to rewrite the whole compiler, Kotlin introduced last year a new IR backend which has become stable and the default compiler in Kotlin 1.5. The new IR backend should improve performance, according to JetBrains, while providing a sound infrastructure to add support for new language features. The new compiler is not the only new feature that Kotlin 1.5 adds to its JVM backend, which also improves type nullability handling and compilation of SAM adapters and lambdas.

Kotlin 1.5 requires at least Android Studio 4.2, or Arctic Fox Canary 15.

Rate this Article