Journal for Developers

Kotlin for Java Virtual Machine

0 103

#Introduction

What exactly is JVM? – Well in very simple terms, the Java Virtual Machine is used on the computers to run the Bytecodes generated by the Java compiler. So does that mean only Java is meant to run the JVM. Kotlin provides many features that aren’t available in Java such as a proper function type, extension functions, or data classes.

#Java Virtual Machine

JVM is a very complex tool and there is a lot to learn about this complex tool, and it has been extensively described in Oracle’s Spec.

It is an abstract virtual computer running on various operating systems, and this what makes Java “platform independent”.

The JVM provides a defined set of instructions, just like any real computer, which can be used by a program and are translated to machine specific instructions by the JVM itself later on.

As described in the JVM Spec, the Java Virtual Machine doesn’t know anything about the programming language Java. Basically, JVM defines the class file in binary format containing the machine code instructions, or we can say bytecodes which are needed to be executed on the machine. The very interesting point to note here is that:

  1. JVM, isn’t only dedicated to Java.
  2. makes us free to choose a technology of our likings as long as we provided proper class files that are complaint to the very strict constraints.
  3. any Java bytecode can interoperate with other Java bytecode on the JVM.

#Creating Class Files

Compiler, is our friend in need, because the process of creating class files from human-readable source code is what a compiler does. One example is Oracle’s Java Compiler shipped with the JDK (javac) that is capable of compiling .javafiles to .class files.

In Addition to Java, Kotlin has emerged as one of the many other JVM languages in the last few years, to provide an alternative abstraction for us developers to create programs for the JVM.

#Kotlin Bytecode Generation

As stated in the official FAQs “Kotlin produces Java compatible bytecode,” which means that the Kotlin compiler is capable of transforming all the nice features into JVM compatible instructions and this can even be observed using IntelliJ IDEA tools.

Let’s look at some examples:

Top Level Functions

Kotlin


fun footer(){}

We can investigate this simple top level function defined in a .kt file using IntelliJ.

Follow the specified path of IntelliJ Idea, “Tools → Kotlin → Show Kotlin Bytecode”, this will open a new window inside the IDE providing a live preview of the Java bytecode the compiler would create for the currently edited .kt file.

Java bytecode


public final class de/swirtz/kotlin/FileKt {
  // access flags 0x19
  public final static foobar()V
    L0
      LINENUMBER 3 L0
      RETURN
    L1
      MAXSTACK = 0
      MAXLOCALS = 0
    @Lkotlin/Metadata;
    // compiled from: File.kt
}

Are you one of the people who can read this file, actually only few people can actually read these files. This is the reason, we can choose the option “Decompile”. This will show us a Java class enclosing the functionality previously described with Kotlin.

Top Level Function decompiled


public final class FileKt {

  public static final void footer() {

  }

}

You just had a glimpse of Kotlin top level class compiled into a final Java class with a static function.

#Conclusion

Kotlin is much more than just abstracting Java’s variables, operators, etc, it also provides so many extensions to existing Java classes like List or String.

Finally, if you want to read more about Kotlin’s beautiful features, there is a wonderful book Kotlin in Action for you!

Subscribe to our Journal

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!