Lets write a simple Kotlin program to display “Hello World” message on the screen. With the help of this simple program we will try to understand the basics of Kotlin Programming.
You can run Kotlin program in Eclipse IDE or the popular IntelliJ IDEA IDE. You can refer the following tutorials to learn, how to create and run your first Kotlin programs in these IDEs.
1. Create and Run Kotlin Project in Eclipse IDE
2. Create and Run Kotlin Project in IntelliJ IDEA IDE
Hello World Program in Kotlin
// Display Hello World! on screen fun main(args : Array<String>) { println("Hello World!") }
Output:
Hello World!
Lets discuss Hello World Program in detail
1. The first line of the program is:
// Display Hello World! on screen
This is a comment. You can write anything here, the compiler ignore these comments while executing the program. Comments improve the code readability so when a programmer reads them, they can easily understand the purpose of code, by just reading the comment.
2. The second line of the program is:
fun main(args : Array<String>) { }
This is the main function. Similar to java, the execution of the Kotlin program starts from this function. This function is the starting point of the Kotlin program. This is the mandatory function of the Kotlin program.
3. The third line of the program is:
println("Hello World!")
This is similar to the System.out.println(“Hello World!”) statement in java. The purpose of this statement to display the message inside double quotes on the screen.
Leave a Reply