Hello, Kotlin
Kotlin is a modern, statically-typed language created by JetBrains that runs on the JVM, meaning it compiles to the same bytecode as Java and can call Java code (and be called from it) directly. It's Google's officially preferred language for Android development, but it also runs as backend code, compiles to JavaScript, and even to native binaries.
Your first program
fun main() {
println("Hello, Kodstigen!")
}
Compare this to Java's Hello example, and the difference is immediate:
- No wrapping class is required,
fun main()is a genuine top-level function, Kotlin doesn't force everything to live inside a class. println(...)instead ofSystem.out.println(...), far less ceremony for something you'll type constantly.- No semicolons required at the end of lines (though they're legal if you want them).
Compiling and running
kotlinc hello.kt -include-runtime -d hello.jar
java -jar hello.jar
kotlinc is the Kotlin compiler, -include-runtime bundles the small Kotlin standard library into the jar so it runs anywhere a JVM exists, without a separate Kotlin install.
TIP
Because Kotlin and Java share the same bytecode target, you can mix .kt and .java files in one project and call between them freely, one of the biggest reasons Kotlin caught on so fast: teams could adopt it file by file instead of rewriting everything at once.