English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this article, you will learn to write a 'Hello World' program in Kotlin.
A simple program that outputs 'Hello, World!' on the screen. Since it is a very simple example program.
Before writing the program, make sure that your computer can run Kotlin.
Let's explore how the "Hello, World!" program is valid in Kotlin.
// Hello World Program fun main(args: Array<String>) { println("Hello, World!") }
When you run this program, the output is:
Hello, World!
// Hello World Program
Any that starts with // The lines at the beginning are comments in Kotlin (similar to Java). Comments are ignored by the compiler and are intended to help readers better understand the program's intent and functionality. For more information, please visit Kotlin Comments.
fun main(args: Array<String>) { ... }
This main function is required in every Kotlin application. The Kotlin compiler starts executing code from the main function.
This function takes a string array as a parameter and returns Unit. You will learn about functions and parameters in later chapters.
Remember that the main function is a mandatory function, which is the entry point for every Kotlin program. The signature of the main function is:
fun main(args: Array<String>) { ... .. ... }
println("Hello, World!")
The println() function prints the given message within quotes and a newline character, and outputs it to the standard output stream. In this program, it prints and outputs Hello, World! followed by a newline.
As you know, Kotlin can be used with Java 100% interconvertible. This is equivalent to Java "Hello, World!" program.
// Hello World Program class HelloWorldKt { public static void main(String[] args) { System.out.println("Hello, World!"); } }
Unlike Java, it is not necessary to create a class (class) in every Kotlin program. This is because the Kotlin compiler creates this class for us.
If you are using IntelliJ IDEA, please go to Run > Edit Configurations to view this type. If you name a Kotlin file HelloWorld.ktIf so, the compiler will create the HelloWorldKt class.
The println() function calls System.out.println() internally.
If you are using IntelliJ IDEA, place the mouse cursor next to println, then go to Navigate > Declaration (shortcut:)Ctrl +BFor Mac:Cmd + BThis will open the Console.kt (declaration file). You can see that the println() function internally calls System.out.println().