Kotlin First Program
Every Kotlin program starts with a main function. Think of the main function as the front door of your program — the computer always walks in through this door first when it runs your code.
The Simplest Kotlin Program
fun main() {
println("Hello, World!")
}Run this code. The output is:
Hello, World!Breaking Down Each Part
┌────────────────────────────────────────┐
│ fun main() { │
│ │ │ │ │
│ │ │ └── Opening curly brace │
│ │ │ starts the function │
│ │ └── main: the function name │
│ │ (always "main" to start) │
│ └── fun: keyword meaning "function" │
│ │
│ println("Hello, World!") │
│ │ │ │ │
│ │ └── text to print │
│ └── built-in function that │
│ prints a line to the screen │
│ │
│ } │
│ └── Closing curly brace │
│ ends the function │
└────────────────────────────────────────┘
println vs print
Kotlin gives you two ways to display text on the screen:
println()— prints the text and moves to a new line after printing.print()— prints the text and stays on the same line.
fun main() {
println("Line 1")
println("Line 2")
print("No ")
print("new line here")
}Output:
Line 1
Line 2
No new line herePrinting Numbers and Calculations
You can print numbers directly without quotes:
fun main() {
println(42)
println(3 + 7)
println(10 * 5)
}Output:
42
10
50How the Program Runs
Your Code (.kt file)
│
▼
Kotlin Compiler (kotlinc)
│
▼
Bytecode (.class files)
│
▼
JVM reads the bytecode
│
▼
Looks for main() function
│
▼
Executes line by line from top to bottom
│
▼
Output appears on screen
The JVM always executes code inside main() from the first line to the last, one line at a time, in order.
Common Beginner Mistakes
Missing quotes around text
// Wrong
println(Hello World)
// Correct
println("Hello World")Misspelling println
// Wrong
printLn("Hi") // uppercase L causes an error
// Correct
println("Hi")Missing curly braces
// Wrong
fun main()
println("Hi")
// Correct
fun main() {
println("Hi")
}Your Turn
Modify the program to print your own name and city:
fun main() {
println("My name is Alex.")
println("I live in Berlin.")
println("I am learning Kotlin.")
}Each println call prints one line. You can add as many lines as you need.
