How Kotlin Code Runs

From .kt to Running Program

main.kt  --kotlinc-->  MainKt.class (JVM bytecode)  --JVM-->  output
                       (same bytecode format Java produces)

Android:  .kt  -->  .class  -->  D8/R8  -->  .dex  -->  runs on Android Runtime (ART)

The Kotlin compiler kotlinc turns your source into the same bytecode that Java produces. Top-level functions in main.kt are placed in a generated class called MainKt. On Android the bytecode is converted to .dex files for the Android Runtime.

main With Arguments

Example

main can also take command-line arguments as an Array<String>. Here we run it without arguments.

fun main(args: Array<String>) {
    println("Number of arguments: ${args.size}")
    if (args.isEmpty()) {
        println("No arguments given, using default city: Hyderabad")
    }
}

Output

Number of arguments: 0
No arguments given, using default city: Hyderabad

Kotlin Targets

TargetOutputUsed for
Kotlin/JVMJVM bytecodeAndroid, Spring Boot, Ktor
Kotlin/JSJavaScriptweb front ends
Kotlin/Nativemachine codeiOS, embedded, CLI tools
Kotlin/WasmWebAssemblyfast web apps

Note: This course uses Kotlin/JVM, the target used by Android and most backends.