Deddy Romnan Rumapea

Apr 14, 2021

5 min read

Learn Kotlin: Hello World!

Write your first Kotlin program on IntelliJ IDEA.

The “Hello, World!” — tradition of every new programmer.

— Prerequisite —

  1. Java Development Kit (JDK). I recommend use OpenJDK from https://adoptopenjdk.net/ . Just choose the latest LTS version and you’re good to go.
  2. IntelliJ IDEA. You can use any version. In this article I’m going to use version 2020.3.2 Community Edition. [Download]
  3. Gradle. This comes built in with IntelliJ IDEA.

— Steps —

Step 1 : open your IntelliJ IDEA.

IntelliJ IDEA’s welcome page

Step 2 : configure the new project.

Project configuration #1
Project configuration #2

Step 3 : wait for the IntelliJ IDEA to setup the project

Importing gradle project

Step 4 : open the main.kt file in 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
main.kt file
Hello world code

Step 5 : running your first Kotlin code

  1. Using the keyboard shortcut Ctrl+Shift+F10 (Windows) or Control+R (macOS).
  2. Clicking on the cute little green triangle button next to line number in the editor. Then click Run ‘MainKt’.

Step 6 : program outputs

Program output

— Code explanation —

This is the code to print out “Hello World!” to the console.
  1. 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.
  2. 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.
  3. 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.
  4. 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.

— Learn Kotlin Series —

  1. Learn Kotlin: Introduction
  2. Learn Kotlin: Hello World! (this article)
  3. Learn Kotlin: Variables
  4. Learn Kotlin: Data Types
  5. Learn Kotlin: Basic Operators
  6. Learn Kotlin: Functions

— Further Reading —

  1. 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).