Kotlin Programming Language Tutorial #1 - Hello world

In this new series we are going to learn Kotlin.

:information_source: Note: this is an unnofficial tutorial. Official tutorial can be found here.

:information_source: Note: this is the first time i’m writing big tutorial post.

What is Kotlin?

Kotlin is a language originally intended for JVM as an alternative to Java.
Kotlin is much more condense than Java, and has additional sugar syntax.

For example, this is a Hello World in java:

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello World!")
    }
}

And this is the same program, in Kotlin:

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

There is visibly less code in Kotlin version.
You don’t really need to know Java in order to learn Kotlin, but i strongly reccomend learning simpler language first (like Python or Lua) for general programming introduction and programming concepts.

Getting started

  1. First of all, you need JDK for executing JVM code. I would recommend Eclipse/Adoptium Temurin and Amazon Corretto. Keep in mind that if you choose Oracle JDK (official JDK from company behind Java), it will require special licensing in production, which you probably don’t want.
  2. Pick an editor of your choice. It should support language servers or have Kotlin support. You can try Visual Studio Code (with relevant plugins, by Microsoft) and IntelliJ IDEA (by JetBrains, the authors of Kotlin). IDEA has two versions, Community and Ultimate. Community is free and open-source, Ultimate is paid and has more features. If you feel like Community isn’t enough but don’t have money to buy Ultimate, EAP program will sometimes give you free access (indicated by No subscription required.) to Ultimate with the cost of stability and occasional bugs, though in my experience no bad crashes or bugs were to happen to me, besides support of plugins from marketplace.

New project

I’m going to assume you chose IntelliJ IDEA for editor since i’m more familiar with it.

Feel free to make a post detailing making a new project in VS Code. I will gladly link it here.

  1. In main window, click New project…


2. Set all parameters like on screenshot. Pick a name and a location where you will store all your projects or all tutorial projects. Choose your downloaded JDK.


3. Click create. In main window, create new Kotlin file. Name it main, and select type File.


4. In the opened editor, type main and press tab. This will add an empty main function. main is an entry point to Kotlin (and Java) programs, and is needed if you want your project to be executable.

image
5. Inside curly brackets add next code:

println("Hello World!")

This is a function call. println is the name of function, and parentheses are arguments to the function. In this case println only has one argument, which is a string of text to output in the console. Strings are made with double quotes like so: "Hello world!". Note that, unlike C or Java, Kotlin doesn’t need semicolons (;) unless you put multiple statements on the same line.
6. Check for any errors. They are indicated by wavy red line and hovering them will show you what’s wrong.
7. Press green triangle next to main or press green triangle at the top in the toolbox. This will compile and run your Kotlin program.
8. You should see console with text Hello World! in it. If so, congratulations! You have made your first kotlin program.

image

Excersices

  • Try changing the text in println.
  • Try printing more lines.

Afterword

Thanks for reading and following this tutorial. See you in the next one!

Edit: forgot to add class in Java Main example