As you already know, Kotlin is a modern, concise, and safe programming language, first invented to be an alternative to Java. As such, Kotlin can be compiled to JVM bytecode to run on the JVM applying the approach "write once, run anywhere".
The JVM doesn't care if the original code is Java, Kotlin, or Scala: if the compiled code is valid bytecode, it will run. Kotlin does more than just solve the problems of Java code. As a multiplatform language, it solves real-world problems, not just some specific issues.
In this topic, we explore the different options for compiling Kotlin code and then focus on how Kotlin code is compiled to bytecode to run on the JVM. Also, we will learn why the knowledge of Kotlin compilation may be useful.
Kotlin compilation
Kotlin is a compiled language, which means the code needs to be compiled before you can run it. Having said that, let's explore the different options for compiling Kotlin code.
Kotlin/JVM | The most common target for Kotlin, with it you can use all the advantages and libraries/ frameworks for the JVM. |
Kotlin/JS | Enables you to run Kotlin code in the browser. |
Kotlin/Native | Sometimes we don't need any virtual machine such as iOS or embedded systems, then Kotlin/ Native comes to rescue. |
This makes Kotlin multiplatform, which allows you to share the same logic on different operating systems, such as macOS, Windows, Linux, Android, iOS, watchOS, and others.
Now that you have a big picture of Kotlin compilation, let's focus on Kotlin/JVM.
Decompile
Kotlin is a concise language, so let's prove that by converting the following UML diagram to Kotlin code:
Here's the Kotlin code:
class Employee(
var name: String,
)Where are the get and set methods? As we've said, Kotlin is a concise language, which means that a large part of the code will be written for you automatically and you don't need to write it – like getters and setters.
To make this code valid to run on the JVM, a lot of details will be added to it, and to see the changes, we need to take a look at the Kotlin code compiled to bytecode, right? Yes, but bytecode is difficult to understand, so we will need to decompile the bytecode back to Java, which is less concise. That will enable us to see what happens to Kotlin code under the hood.
To see the code converted to Java, we can use a great tool IntelliJ IDEA offers for that. After entering the above code in your IDE, go to Tools >> Kotlin>> Show Kotlin bytecode. In the opened window, click "Decompile".
A new file will be opened with the .java extension. Let's focus on the most important thing in this code, which is the Employee class.
Kotlin code vs. Java code
Here is the code after decompiling to Java:
public final class Employee {
@NotNull
private String name; // private attribute
@NotNull
public final String getName() { // get method
return this.name;
}
public final void setName(@NotNull String var1) { // set method
Intrinsics.checkNotNullParameter(var1, "<set-?>");
this.name = var1;
}
public Employee(@NotNull String name) { // constructor
Intrinsics.checkNotNullParameter(name, "name");
super();
this.name = name;
}
}All this additional code, including set, get, and the constructor, will be added for you so that it will be valid bytecode that can run on the JVM.
Now, why is this knowledge useful? In most cases, you will target the JVM and you may use libraries that were first made for Java. As Kotlin is concise, some problems may occur if you don't pay attention to that. For example, you may use annotation from a framework or library that works only with the get method. Meanwhile, the get and set methods are written for us automatically. How can we specify them? Kotlin has a solution for these problems, which we will cover in the following topics.
Conclusion
Today we've learned that Kotlin can target different platforms, which enables Kotlin to solve numerous problems. We've also learned how to decompile Kotlin code to Java and saw that Kotlin is really a concise and clean language. With this knowledge, we will be able to solve a lot of problems in the following topics. Now let's practice.