Hey people! Welcome to my blog on Kotlin and it is interoperable with java.

1. How kotlin is interoperable with java

Kotlin is interoperable with java programming language, because just like java compiler similarly kotlin compiler will generate byte code which can run on Java Virtual Machine makes kotlin interoperability with java.
With this feature we will be able to call functions and any properties from kotlin in java files and similarly java in kotlin files.
Now let’s get bit technical with picture below which clearly outlines what we need to understand.

Preview image for article
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 :

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 :

Kotlin has it’s own compiler. Once both files get’s compiled .class files will be generated.
This .class files contains byte code.
Compiled file will be changed from Hello.kt to HelloKt.class
Later we will get to know more about byte code.

Step 3 :

All those .class files will be packaged inside .jar and executed. Jar means java archive which includes Java-specific manifest file. They are built on the ZIP format and have a .jar file extension.
Many class files and resources will be packaged into single file for distribution.
All those files will be in byte code format which can be run on any java virtual machine and this archive format is cross platform.

Step 4:

We need Java Virtual Machine to execute that code and when the execution happens, JVM will translate the byte code into platform specific application code.

But code compiled with kotlin compiler depends on Kotlin Runtime Library. This library contains the definitions of kotlin own standard library classes and extensions that kotlin adds to java API’s.

3. Which platforms can we target

We will be able to target as many as platforms which makes use of Java Virtual Machine.
As we know kotlin key feature is supporting Multiplatform, we code once common kotlin code and we compile it to platform specific code.
With platform specific code, kotlin right now supports :

  • Server side
  • Android / IOS
  • JavaScript
  • Native
  • Data science

4. Java to kotlin converter

Jetbrains IDE – IntelliJ IDEA comes with handy tool which can convert any java file into kotlin file.

This option can be found by selecting(right click) the java file and look for item Convert Java File to Kotlin File.
Alternatively you can just use windows shortcut key Ctrl + Alt + Shift + k after selecting the file.

Let’s convert the earlier java file which has prints a function.
This is the result after converting.

import kotlin.jvm.JvmStatic

object Hello {
    fun main(args: Array<String>) {

Above code looks bit different while compared to ours because it is auto-generated, but code will definitely get’s compiled.

5. How byte code looks and loads

We won’t be able to understand the byte code since it is converted to low level code. Java Virtual Machine will take care of the reading and executing that code.
But here’s how a function which prints a string will look like.

Image from ide which shows byte code

You can try this yourself from your IDE.

Show kotlin bytecode

6. References

Kotlin multiplatform
Kotlin programming language
Kotlin interoperability documentation

Sindhu Reddy.C

Sindhu is a student, pursuing her Bachelor of Technology in Computer science and Engineering. She is a technoid interested in Python, UI/UX Design Android Development and Java.

Connect with me

Here We Go Again : (

if (article == helpful) {
    println("Like and subscribe to blog newsletter.")
} else {
    println("Let me know what i should blog on.")

This site uses Akismet to reduce spam. Learn how your comment data is processed.