LAST UPDATED: AUGUST 10, 2020
First Program in Kotlin using IntelliJ IDEA
In the last tutorial, we saw how to install IntelliJ idea in our system. In this tutorial, we will see how to create a new project in IntelliJ IDEA IDE and run our first program in Kotlin which will print "Hello World" on the console.
Create a new project in IntelliJ IDEA
Setting up a project in IntelliJ is necessary to run any Kotlin file. Follow these steps to create a new project:
-
Go to start, search IntelliJ IDEA, and open it. If you are running it the first time you might see some first-time settings like import settings and select a dark or normal theme. Choose it accordingly.
-
Next, you'll see this window:
-
Click on "Create New Project".
-
Choose Kotlin from the left side panel and select "JVM| IDEA" option. Click on next.
-
Enter the name of the project. You can edit the project location and provide a custom location.
-
On the left side, you can see the project structure like this:
-
Right click on src folder and select New > Kotlin File/Class and hit enter. You’ll see:
-
Enter file name "HelloWorld" and hit enter.
You have successfully created a new Kotlin project and a Kotlin file in it. Now we'll create our first Kotlin program in it.
First program in Kotlin
Write the following program is HelloWorld.kt file:
fun main() {
println("Hello World")
}
Explanation:
-
Line 1: Every Kotlin program starts with the main()
function. Functions are created using the fun
keyword in Kotlin. We'll read more about functions in later sections.
-
Line 2: println()
function is used to print strings on the console.
To run this program click on the green arrow just beside main()
function as shown here:
The program will be compiled and output will be printed on the console:
Summary
In this article, we created a Kotlin project and run our first program in Kotlin. In the next tutorial, we see how to run the same program through the command line.