in , ,

Kotlin Hello World – You First Kotlin Program

Kotlin Hello World - You First Kotlin Program
Kotlin Hello World - You First Kotlin Program

In this tutorial, you will learn to write the Hello World program in Kotlin.

A “Hello, World!” is a basic program that outputs Hello, World! on the screen. Since it’s an exceptionally basic program, it’s often used to introduce another programming language.

Let’s explore how “Hello, World!” program works in Kotlin.


Kotlin “Hello, World!” Program

Lets write a simple Kotlin program to display “Hello World” message on the screen. With the assistance of this simple program we will attempt to understand the basics of Kotlin Programming.

// Hello World Program

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

When you run the program, the output will be:

Hello, World!

How this program works?

  1. // Hello World Program

Any line beginning with//is a comment in Kotlin (like Java). Comments are overlooked by the compiler. They are intended for the person reading the code to more readily comprehend the intent and usefulness of the program. To find out additional, visit Kotlin comments.

2. fun main(args : Array) { … }

This is the main function, which is required in each Kotlin application. The Kotlin compiler begins executing the code from the main function.

The function takes an array of strings as a parameter and returns Unit. You will learn about functions and parameters in later chapters.

For the present, simply recollect that the main function is a mandatory function which is the entry point of each Kotlin program. The signature of the main function is:

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

3. println(“Hello, World!”)

The println() work prints the given message inside the quotation marks and newline to the standard output stream. In this program, it prints Hello, World! what’s more, new line.


Comparison With Java “Hello, World!” program

As you probably are aware, Kotlin is 100% interoperable with Java. Here’s an identical Java “Hello, World!” program.

// Hello World Program

class HelloWorldKt {
    public static void main(String[] args) {
        System.out.println("Hello, World!"); 
    }
}

Few Important Notes

  1. Unlike Java, it isn’t compulsory to create a class in each Kotlin program. This is on the grounds that the Kotlin compiler creates the class for us.

On the off chance that you are using IntelliJ IDEA, go to Run > Edit Configurations to see this class. In the event that you named your Kotlin file HelloWorld.kt, the compiler creates HelloWorldKt class.

2. The println() function calls System.out.println() inside.

On the off chance that you are using IntelliJ IDEA, put your mouse cursor close 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 inside calling System.out.println().


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

Rabin-Karp Algorithm

Rabin-Karp Algorithm

Kotlin Variables Types

Kotlin Variables and Basic Types