Essay

Kotlin and Java Interoperability

Kotlin and Java can live in the same codebase cleanly, which makes migration and mixed-language projects practical.

This piece is archived here for continuity. The original canonical publication lives on Medium.

Kotlin is designed to be fully interoperable with Java. You can use Kotlin code in a Java project and Java code in a Kotlin project, which lets you adopt the strengths of both languages without a rewrite-first strategy.

Using Kotlin in a Java project

One of the practical benefits of Kotlin’s interoperability is that you can introduce it into an existing Java codebase incrementally. This is useful when you want Kotlin’s more concise syntax but do not want to rewrite the system in one go.

To use Kotlin code in a Java project, include the Kotlin runtime on the classpath:

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

Once the runtime is available, you can create Kotlin source files and call them from Java like any other class:

// Kotlin class
class KotlinClass {
    fun doSomething() {
        println("Doing something in Kotlin")
    }
}
// Java code
KotlinClass kotlinClass = new KotlinClass();
kotlinClass.doSomething();

Using Java in a Kotlin project

The reverse also works cleanly. If you already have a Java library or older Java modules, Kotlin can call into them directly.

Include the Java dependency in the project:

implementation "com.example:java-library:1.0"

Then use it from Kotlin:

// Java class
public class JavaClass {
    public void doSomething() {
        System.out.println("Doing something in Java");
    }
}
// Kotlin code
val javaClass = JavaClass()
javaClass.doSomething()

Conclusion

Kotlin and Java interoperability is what makes mixed-language migration practical. You do not need to choose between a clean-room rewrite and staying put forever. You can move gradually, keep the JVM ecosystem, and introduce Kotlin where its expressiveness improves the code most.