A "Hello, World!" is a simple program that outputs Hello, World!
on the screen. Since it's a very simple program, it's often used to introduce a new programming language.
Let's explore how "Hello, World!" program works in Kotlin.
// Hello World Program
fun main(args : Array<String>) {
println("Hello, World!")
}
When you run the program, the output will be:
Hello, World!
// Hello World Program
//
is a comment in Kotlin (similar to Java). Comments are ignored by the compiler. They are intended for person reading the code to better understand the intent and functionality of the program. To learn more, visit Kotlin comments.fun main(args : Array<String>) { ... }
main
function, which is mandatory in every Kotlin application. The Kotlin compiler starts executing the code from the main
function.main
function is a mandatory function which is the entry point of every Kotlin program. The signature of main
function is:fun main(args : Array<String>) {
... .. ...
}println("Hello, World!")
println()
function prints the given message inside the quotation marks and newline to the standard output stream. In this program, it prints Hello, World!
and new line.As you know, Kotlin is 100% interoperable with Java.
// Hello World Program
class HelloWorldKt {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
class
in every Kotlin program. It's because the Kotlin compiler creates the class for us.Run
> Edit Configurations
to view this class. If you named your Kotlin file HelloWorld.kt, the compiler creates HelloWorldKt class.println()
function calls System.out.println()
internally.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()
.