Home > Software engineering >  How does Kotlin code executes internally?
How does Kotlin code executes internally?

Time:09-28

I want to know about the step-by-step internal process that happens on the execution of a Kotlin program. I know about java program execution but wondering what Kotlin does in addition to that?

CodePudding user response:

There is nothing in addition to that.

Kotlin/JVM is compiled down to Java bytecode, which is run by the JVM in exactly the same way as code compiled from Java or Clojure or Scala or Groovy or any other JVM language. The JVM doesn't know what language each class has been compiled from; it just starts up, loads your main class, and executes it, loading up other classes as needed.

One thing to be aware of, though, is that Kotlin has its own (small) standard library. This is also Java bytecode, and must be available to the JVM in addition to your compiled code and the normal Java standard library:

  • If you launch your app using the kotlin program, that adds the Kotlin runtime to the classpath before handing control to the JVM.
  • If you package your code into a .jar file, the Kotlin runtime library will often be included in that.
  • Otherwise, you'll need to tell the JVM where to find it (by adding it into the classpath).

(Of course, all this applies only to Kotlin/JVM; Kotlin/JS and Kotlin/Native are compiled and run in completely different ways.)

  • Related