HomeKotlinKotlin for Java Virtual Machine

Kotlin for Java Virtual Machine

#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!

RELATED ARTICLES

Most Popular