Your First Kotlin Program
Prerequisites: Installing Kotlin
Every Kotlin program starts in a function called main:
fun main() {
println("Hello, world!")
}
Run it with the ▶ button in your IDE, or from a terminal:
kotlinc hello.kt -include-runtime -d hello.jar
java -jar hello.jar
Expected output
Hello, world!
Line by line:
fun main()declares the main function — the program's starting point.funis the keyword for a function.- The curly braces
{ }hold the function's code. printlnprints a value followed by a new line. (printprints without the new line.)"Hello, world!"is a string — text in double quotes.- No semicolons are needed at the end of lines.
A slightly longer program runs line by line, top to bottom:
fun main() {
println("Hello, world!")
println("I am learning Kotlin.")
println(2 + 3)
}
Hello, world!
I am learning Kotlin.
5
Common mistakes
- Writing
PrintlnorPrintLn. Kotlin is case-sensitive:Unresolved reference 'Println'. - Using single quotes for text.
'Hello'is an error — single quotes are only for a single character, such as'A'.
Practice
Write a program that prints your name on one line and your favorite food on the next.
Show solution
fun main() {
println("Ada")
println("Pizza")
}