Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Kotlin
Stage 5: Specialization

Part 01 / Topic 7 of 9

Source → bytecode → JVM

Recap of section 1: kotlinc compiles, then something runs the result. Three things happen between saving your file and seeing output:

Hello.kt  ──kotlinc──▶  HelloKt.class  ──JVM──▶  your output
(source)                (bytecode)             (via `java` or `kotlin`)

Kotlin bytecode is JVM bytecode, the exact same format javac produces, so the JDK's own disassembler, javap, reads it too. Compile examples/01_hello.kt without -include-runtime this time, so the .class file is left on its own:

$ kotlinc 01_hello.kt -d out01
$ javap -p out01/_01_helloKt.class
Compiled from "01_hello.kt"
public final class _01_helloKt {
  public static final void main();
  public static void main(java.lang.String[]);
}

Two things stand out, both real, not a simplification:

  1. The class name. You never wrote a class, yet kotlinc made one. The rule is: a file named X.kt compiles its top-level functions into a class named XKt. A clean file named Hello.kt becomes HelloKt; this course's numbered file 01_hello.kt starts with a digit, which can't begin a Java/Kotlin identifier, so kotlinc prefixes an underscore and produces _01_helloKt instead — still exactly the same rule, just working around a filename a class name can't legally start with.
  2. No constructor. A javac-compiled Java class always gets a default constructor whether you write one or not (see the sibling Java course's Part 1). This Kotlin-generated class has none at all in javap's output — it exists purely to hold main, and is never meant to be instantiated.

javap -c shows the real instructions inside main():

$ javap -c out01/_01_helloKt.class
  public static final void main();
    Code:
       0: ldc           #8                  // String Hello, Kotlin!
       2: getstatic     #14                 // Field java/lang/System.out:Ljava/io/PrintStream;
       5: swap
       6: invokevirtual #20                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
       9: ldc           #22                 // String Welcome to Kotlin Step by Step
      11: getstatic     #14                 // Field java/lang/System.out:Ljava/io/PrintStream;
      14: swap
      15: invokevirtual #20                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      18: return

Each println(...) call became four bytecode steps: load the string (ldc), get System.out (getstatic), swap the two values on the stack so they're in the right order for the next step, then call println on System.out (invokevirtual). swap has no equivalent in the Java version of this same program (Java's compiler pushes System.out first, so it never needs to reorder); Kotlin's println is a top-level stdlib function that the compiler inlines straight into a call to System.out.println, and that inlining is what produces the extra swap. The main(String[]) overload kotlinc always generates just forwards to the no-argument main() you wrote:

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #25                 // Method main:()V
       3: return

This is why fun main() (no parameter) and fun main(args: Array<String>) are both valid in Kotlin — whichever one you write, kotlinc makes sure a main(String[]) exists for the JVM to call.

🔍 Going deeper: javap -c output is a JDK/kotlinc detail, not a language guarantee — it can change between compiler versions, same as javac's output can. You never write or edit it by hand.

Full program: examples/01_hello.kt.

Done here? The practice set for Part 1 — including the solutions — is on the part page.

Search the guide