Kotlin Hello World – Your First Kotlin Program

Hello, World! is the first basic program in any programming language. Let’s write the first program in Kotlin programming language. 

The “Hello, World!” program in Kotlin – 

Open your favorite editor notepad or notepad++ and create a file named firstapp.kt with the following code. 

// Kotlin Hello World Program
fun main(args: Array<String>) {
    println("Hello, World!")

You can compile the program in command line compiler. 

$ kotlinc firstapp.kt

Now Run the program to see the output in command line compiler. 

$kotlin firstapp.kt
Hello, World!

You can run the program in Intellij IDEA

Details about the “Hello, World!” program –

Line #1: First line is a comment which is ignored by the compiler. Comments are added in program with the purpose of making the source code easier for readers to understand. 

Kotlin supports two type of comments –

1. Single line comment

  // single line comment

2. Multiple line comment

                           /*   This is
                                multi line

Line #2: 
The second line defines the main function 

 fun main(args: Array<String>) {
    // ...

The main() function is the entry point of every program. All functions in kotlin start fun keyword followed by the name of function(here main is the name), a list of parameters, an optional return type and the body of the function ( { ……. } ). 
In this case, main function contains the argument – an array of strings and return units. Unit type corresponds to void in java means the function does not return any value. 

Line #3: 
The third line is a statement and it prints “Hello, World!” to standard output of the program. 

println("Hello, World!")

Semicolons are optional in Kotlin, like other modern programming languages.

Few Important Notes

  1. Unlike Java, it is not mandatory to create a class in every Kotlin program. It’s because the Kotlin compiler creates the class for us.

    If you are using IntelliJ IDEA, go to Run > Edit Configurations to view this class. If you named your Kotlin file HelloWorld.kt, the compiler creates HelloWorldKt class.
  2. The println() function calls System.out.println() internally.

If you are using IntelliJ IDEA, put your mouse cursor next to println and go to  Navigate > Declaration ( Shortcut: Ctrl + B . For Mac: Cmd + B), this will open Console.kt (declaration file). You can see that println() function is internally calling System.out.println().

I hope you found what you were looking for from this tutorial. If you want more Kotlin tutorials like this, then do join our Telegram channel for future updates.

Thanks for reading, have a nice day 🙂

Leave a Comment

Your email address will not be published.