First Kotlin Program “Hello World”
When starting to learn programming languages, we always start with the same example. Let’s start our Kotlin programming article with the “Hello World” program without breaking tradition.
You can copy the code you see below and paste it into your editor, or you can go to the “Kotlin Playground” page via your Web browser and try the codes there.
// First program in kotlin
fun main() {
println("Hello World")
}
Explanation:
The line that starts with the //
sign is the comment line. The compiler does not accept this line as code. It is used to take notes or make explanations about which code does what.
The fun
expression refers to the beginning of a function.
main()
is the entry point of Kotlin programs. It is case sensitive.
A code block is written between { }
signs.
println(“Hello World”)
is a code expression you see here. When we send the “Hello World” parameter to the println() function, it will write the sent parameter to the screen and move to the next line. If you want, you can print more than one line under each other. Here is an example.
// First program in kotlin
fun main() {
println("Hello World")
println("I am the first Kotlin Program")
}
When we run the example above, you should get the following output.
Hello World
I am the first Kotlin Program
Process finished with exit code 0