Site icon Developers Breach

How kotlin is interoperable with java

Hey people! Welcome to my blog on Kotlin interoperability with java.

1. How kotlin is interoperable with java

Kotlin is interoperable with the Java programming language because, much like the Java compiler, the Kotlin compiler produces JVM bytecode. This means Kotlin code can run on the Java Virtual Machine (JVM), right alongside Java classes.

In practice, this enables you to:

Because both languages ultimately compile to .class files, the JVM can load them transparently, no matter which language they originated from.

Real-World Implications

Now let’s get bit technical with picture below which clearly outlines what we need to understand. It outlines how both Kotlin and Java source code are compiled and packaged into a single application.

Featured image

2. How code get’s compiled

Keep referring to image above and step numbers to the bottom of the image.
We have both kotlin and java comparing along top and bottom.

Step 1 : Source Code
Both Hello.kt and Hello.java files have source code which needs to get compiled. So let’s just say we have hello world functions as our source code in those files.

Kotlin :

fun main() {
    println("Hello Kotlin")
}

Java :

public class Hello {

    public static void main(String[] args) {
        System.out.println("Hello Java");
    }
}

Now let’s compile this code.

Step 2: Compilation

Note: Kotlin code also depends on the Kotlin Standard Library (and possibly the Kotlin runtime). These libraries provide Kotlin-specific features (e.g., coroutines, extension functions, etc.) that Java doesn’t natively have.

Step 3: Packaging into a JAR

Step 4: Execution on the JVM

3. Which platforms can we target

Since Kotlin (like Java) compiles to JVM bytecode, you can run it on any platform that supports a JVM, including desktop, server, and especially Android.

Beyond the JVM, Kotlin has multiplatform capabilities:

Key Point:

4. Java to kotlin converter

JetBrains’ IntelliJ IDEA and Android Studio include a built-in Java to Kotlin converter. This tool allows you to quickly transform a .java file into a .kt file, preserving the class’s logic but rewriting it in Kotlin syntax.
You can do this from refactor tools choose “Convert Java File to Kotlin File”

Example Conversion

Original Java file:

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello");
    }
}

Converted Kotlin :

import kotlin.jvm.JvmStatic

object Hello {
    @JvmStatic
    fun main(args: Array<String>) {
        println("Hello")
    }
}

Observations:

Note: The converter does an excellent job for most simple classes but might produce less-than-idiomatic Kotlin code if you rely heavily on Java-specific patterns (e.g., checked exceptions, certain reflection patterns). You can refine the converted code manually to leverage Kotlin’s data classes, extension functions, or coroutines.

5. How byte code looks and loads

Once compiled by either kotlinc or javac, Kotlin and Java produce .class files with JVM bytecode. Bytecode is a low-level, human-unfriendly representation of your logic. The JVM reads and executes it, but developers typically don’t manipulate .class files directly.

You can use IDE tools (e.g., IntelliJ’s “Show Kotlin Bytecode”) to see what your Kotlin or Java code looks like at the bytecode level.
But here’s how a function which prints a string will look like.

Execution and Loading

You can try this yourself from your IDE.

6. References

Kotlin multiplatform
Kotlin programming language
Kotlin interoperability documentation

Author

Reviewer & Editor

Exit mobile version