Hello World – Kotlin

Welcome to the first tutorial of Kotlin. We will be starting Kotlin from scratch. Our first program will be similar to the Hello World Java program but in Kotlin programming language. We will be using the IntelliJ IDEA for writing Kotlin. If you are not familiar with IntelliJ IDEA, check out their website.

Download and install IntelliJ IDEA. After installing, run the program and the Welcome Screen appears after loading.

I am using IntelliJ IDEA Version 2018.3.5. Click on Create New Project. A New Project Window appears.

Note: Java Development Kit (JDK) latest should be installed on your OS. Kotlin is designed to interoperate fully with Java, and the Java Virtual Machine (JVM) due to which Java JDK & JVM are required.

Select Java from the Menu and check Kotlin/JVM from the Additional Libraries and Frameworks section. Press Next and the Project Details window appears

In the Project Details window, enter the name of the project as Hello World in the Project name field and set the location for the project to be saved on your local hard drive by clicking on the icon with the 3 dots (…). Click Finish and the IDE appears

Select the src folder in the Project menu. Right Click and select New and then select Kotlin File/Class to create a new Kotlin file with an extension of HelloWorld.kt

A dialog will appear to request essential data like Name of the File/Class and the Kind. Enter the name Hello World, and leave the Kind to File. Press OK ۔

A new Kotlin file/class is created under the src (Source) folder. In Kotlin it is not important to declare a class name and write functions within it. Functions can be written directly into a Kotlin file and executed. The main method in java is also important in Kotlin but written in a different way. Within the open file, HelloWorld.kt write the following code

fun main() {
     println("Hello, World")
}

After writing, Press Alt + F10 or just the play button in the right top corner menu to run the program. If the button is grayed out then use the button to the right of the main function, click and select ‘Run HelloWorldKt’ from the dropdown menu. Below a console will display the result.

Congratulations, you’ve created your first Kotlin program. Try taking the challenge below to practice. The only way to be a better programmer is to practice.

Challenge: Try printing your name in the console.

You can find the complete tutorial code here.

Tell us in the comments on how to improve this article and help developers learn better.