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:
- The class name. You never wrote a class, yet
kotlincmade one. The rule is: a file namedX.ktcompiles its top-level functions into a class namedXKt. A clean file namedHello.ktbecomesHelloKt; this course's numbered file01_hello.ktstarts with a digit, which can't begin a Java/Kotlin identifier, sokotlincprefixes an underscore and produces_01_helloKtinstead — still exactly the same rule, just working around a filename a class name can't legally start with. - 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 injavap's output — it exists purely to holdmain, 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 -coutput is a JDK/kotlincdetail, not a language guarantee — it can change between compiler versions, same asjavac'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.