Writing Kotlin Hello World program

Whether you are a new programmer or starting to give a try to a new programming language, the general practice is running a simple program, the “Hello World”.

The same applies to Kotlin. Chances are, this is not your first language to start with. Most probably, you are experienced with Java or some other language and going to give a try to Kotlin.

Hello world program in Kotlin

The Kotlin code below simply displays the “Hello World” on the screen and this is followed by explaining code lines in that program:

fun main(args: Array<String>) {

    //The demo of Hello World in Kotlin

    println("Hello World!")

}

The output:

Hello World!

Explaining the above code line by line

  • The Kotlin file is saved as .kt
  • The keyword fun means the function in the code.
  • main is the function name which is the entry point of any Kotlin program. This is a mandatory function that takes an array of strings as argument.
  • If you are using Kotlin 1.3 and your program does not need command line arguments then you may omit this. So, the “Hello World!” program can be written as:
fun main() {

//The demo of Hello World in Kotlin

    println("Hello World!")

}
  • The second line of code in the above code is a comment that starts with // (double forward slash).
  • You may also write block comments starting with /* and ending at */
  • The println() is a function that is used to display the output. The ‘ln’ in ‘println’ function adds a line after displaying the “Hello World!” in the above code.
  • You may also use print() and it will not add a newline as the output.
Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!