Learn Kotlin: Hello World!
Hey there! In this article I’m going to show you how to make your first program in Kotlin on IntelliJ IDEA.
The program that we are going to write is pretty simple. Its only purpose is to print a simple “Hello World!” text to the screen. The goal is to illustrate the basic syntax of Kotlin and how to run the code.
— Prerequisite —
- Java Development Kit (JDK). I recommend use OpenJDK from https://adoptopenjdk.net/ . Just choose the latest LTS version and you’re good to go.
- IntelliJ IDEA. You can use any version. In this article I’m going to use version 2020.3.2 Community Edition. [Download]
- Gradle. This comes built in with IntelliJ IDEA.
After you have installed those on your machine, let’s get started!
— Steps —
Step 1 : open your IntelliJ IDEA.
The first screen you are going to see is the welcome page of IntelliJ IDEA. Click on New Project button.
Step 2 : configure the new project.
On the left side, choose Kotlin as the build tools. And choose Console Application for the Project Template.Then you can follow all the configuration as in this following picture. When you’re done, hit Next.
For the next configuration, just leave it as default. Then click Finish.
Step 3 : wait for the IntelliJ IDEA to setup the project
This is going to take 30 seconds to 2 minutes depending on your computer speed.
Step 4 : open the main.kt file in your project
IntelliJ IDEA already set up some files for your project.
- Open up you project explorer by clicking Project button on the left. Click twice to open a directory/folder.
- Go to HelloWorld > src > main > kotlin > main.kt
Here you will find the code has already written.
If it’s still blank or you want to write the code yourself, you can type in the following code to the editor on the right. Editor is where we write our code.
Step 5 : running your first Kotlin code
You can run your code in two ways :
- Using the keyboard shortcut Ctrl+Shift+F10 (Windows) or Control+R (macOS).
- Clicking on the cute little green triangle button next to line number in the editor. Then click Run ‘MainKt’.
You will see loading indicator on bottom right that says “building”.
Step 6 : program outputs
After IntelliJ IDEA finished compiling the code, you should see the program output on the bottom. If it’s not automatically opened, then click on Run button on bottom left.
Here you can see the program prints out the message “Hello World!” to the screen. Pretty neat!
— Code explanation —
You might have no idea or confused by the code that we wrote earlier. And that’s totally okay. I will try my best to give you a little explanation that hopefully shed some lights for you.
Consider this picture.
There are four things I highlighted in the picture:
- fun is a keyword to define a function. We use that keyword to create a function. In this case, the function is basically created to be a place where we put our code. However that’s a oversimplified definition of a function. We are going to learn more about function later.
- main is the name of the function we created. Why is it named main? Well it’s just because that’s the main component of our program. You are free to name your function whatever you like. The parantheses are where we want to put parameters, in this case there are none, thus just opening and closing parantheses with nothing in between. Parameters are just data that is needed to run the program.
- println is the name of a function that is already built in with Kotlin. We don’t create it, it’s just already there. And here we use it to print out a text into the console. So that’s the purpose of the function, to print out a line of text, thus the name println stands for print line.
- Now we see there is a parantheses again, but this time it has something in between. It has “Hello World” in it, which is the text that gets printed into the console. This is called an argument, which is the data you give to a parameter. In this case, the data you give is a string “Hello World”, which the println function use to print into the console. String is simply another name for text. We are going to talk about Strings and other data types later.
Don’t worry if you don’t understand all of this completely for now. I’m trying to explain it just to give you some big pictures of what the code we wrote does. You don’t need to understand it fully because we are going to learn all of them step by step in the next lessons.
Thank you for accompanying me through this lesson. I’m happy you made it till the end. If you have questions or feedback feel free to leave comments. See you in the next lesson!
— Learn Kotlin Series —
— Further Reading —
- What is “Hello, World!”? Why do we use it? You can read on this Wikipedia page (PS : this is trivial, just read it when you have free time).