How Kotlin Code Runs
Prerequisites: Your first Kotlin program
Kotlin is a compiled language. The Kotlin compiler (kotlinc)
checks your whole program and translates it into JVM bytecode —
.class files, often packaged into a .jar. The Java Virtual
Machine then runs that bytecode, compiling the busiest parts to native
machine code as the program runs.
hello.kt → kotlinc → bytecode (.class / .jar) → JVM → runs
Real-world analogy: the compiler is an editor who checks and translates your whole manuscript before printing. The JVM is a printing press found in almost every print shop — so the same printed file works everywhere.
Errors appear before anything runs
Because the compiler checks everything first, many mistakes are caught before the program runs at all:
fun main() {
println("This line would run")
val age: Int = "twenty"
}
hello.kt:3:20: error: initializer type mismatch: expected 'Int', actual 'String'.
Nothing is printed — the program never compiled. The message gives the
file, the line (3), the column (20), and the problem.
Runtime errors
Some problems can only be detected while the program runs, such as converting text that isn't a number:
fun main() {
println("Start")
val n = "abc".toInt()
println(n)
}
Start
Exception in thread "main" java.lang.NumberFormatException: For input string: "abc"
The first line ran, then the program stopped with an exception. The message shows the kind of error and its cause. (The real output also lists a stack trace — the chain of function calls that led to the error.) You'll learn how to handle these in the Error Handling lesson.
Other targets
The same Kotlin code can also be compiled to native binaries (Kotlin/ Native) or JavaScript and WebAssembly (Kotlin/JS, Kotlin/Wasm). This course focuses on the JVM, which is what Android and most back ends use.