Hello World in Kotlin
Your first Kotlin program - the classic Hello World example with Docker setup
Every programming journey starts with Hello World. Let’s write our first Kotlin program.
The Code
Create a file named Hello.kt:
| |
Understanding the Code
fun main()- The entry point of a Kotlin program.fundeclares a function. Unlike Java, no class wrapper is required.println()- Prints to standard output with a newline. It’s a top-level function, notSystem.out.println().- No semicolons - Kotlin doesn’t require semicolons at the end of statements.
Running with Docker
The easiest way to run this without installing Kotlin locally:
| |
Running Locally
If you have Kotlin installed:
| |
Or run directly with the Kotlin script runner:
| |
Expected Output
Hello, World!
Key Concepts
- Top-level functions -
main()doesn’t need to be inside a class - No semicolons - Line breaks typically end statements
funkeyword - Declares functions (likedefin Python)- JVM-based - Compiles to Java bytecode, runs on the JVM
- Concise syntax - Much less boilerplate than Java
Kotlin vs. Java Hello World
Java version:
| |
Kotlin version:
| |
Kotlin eliminates:
- The class wrapper requirement
- The
publickeyword (it’s the default) - The
String[] argsparameter (optional in Kotlin) System.out.prefix
A Slightly More Complex Example
| |
This demonstrates:
val- Immutable variable (likefinalin Java)- String templates -
$nameembeds variables directly in strings
You can also use expressions in templates:
| |
Next Steps
Continue to Variables and Data Types to learn about Kotlin’s type system and null safety.
Running Today
Last updated: