Variables
Store a whole number in a variable and print it.
1fun main() {
2 val five = 5
3 println(five)
4}
$ kotlinc Main.kt -include-runtime -d Main.jar
$ java -jar Main.jar
5
Every line, explained
fun main() {- Every Kotlin program starts at the main function. When you run the program, Kotlin looks for fun main() and runs everything between its { and } from top to bottom.
val five = 5- val creates a read-only variable: a named box whose value is set once and then never changes. Kotlin people reach for val by default, and only switch to var when a value truly needs to change. Here the variable is called five and it stores the value 5. Notice there is no type word like Int: Kotlin sees the value and works out the type for you. This is called type inference.
println(five)- println(...) prints whatever is inside the round brackets to the terminal, then moves to a new line. All lowercase, and no semicolon at the end; Kotlin does not need them. No quotes around five, so Kotlin prints the value stored in the variable, not the word five.
}- This closing curly brace } ends the main function. In Kotlin every opening brace { must have a matching closing brace }.