A Friend in Need Is a Friend Indeed: Kotlin and Java

From JVMLangSummit
Jump to navigationJump to search

Kotlin is a modern programming language compiled to JVM byte codes. The project is started by JetBrains and is distributed under the Apache 2 Open Source License.

Java interoperability is the top priority for Kotlin, because it is intended to be used in existing projects, where Java and Kotlin code will live side-by-side and frequently talk to each other.

In this session we describe the mechanisms of interaction between Kotlin and Java.

Section 1. Enhancing JDK APIs for use in Kotlin.

Adding new functions for JDK APIs is easy with Kotlin’s extension functions. A more difficult thing is seamless interoperability with JDK collections: Kotlin has to introduce declaration-site variance to java.util.* interfaces by virtually splitting each of them in two, and then do some compile-time conversions. No runtime overhead, no headache for the user.

Section 2. Null-safety from the interoperability point of view.

In a Java API, anything may be null, so using it from Kotlin becomes full of null-checks, many of which are pointless (according to the contracts described in the JavaDocs). Kotlin addresses this problem with a number of techniques that allow the user to communicate the knowledge about these contracts to the compiler.

Section 3. Packages and Traits.

Packages and Traits are two concepts in Kotlin that put the code where Java 7 doesn’t allow it: into package-level functions and interface methods. To achieve this, Kotlin has to play tricks that appear to be not so straightforward because of binary compatibility issues and requirements of the debugger interface.


Resources:


Useful links: