The IntelliJ IDEA development environment from JetBrains can be used to develop Kotlin applications. This environment is available for Windows as well as MacOS and Linux. There is a free version – Community, and a paid one – Ultimate offered. For learning purposes this is Free version is completely sufficient. We download these from the website https://www.jetbrains.com/idea/download/other.html .
Installing IntelliJ IDEA
First we start the installer and in the welcome window we click on the “Next” button.
The path on the computer on which the environment is installed is then displayed. You can keep the default installation path or, if desired, define your own installation location. We then click on the “Next” button.
In this step a window opens for various configuration options. Here it is possible to connect the environment with specific file types or to configure the creation of environment icons on the desktop. However, in this particular case it is sufficient to simply click “Next”.
In the next step, a window opens for selecting a directory in the start menu where the application can later be found. We leave the default value and click the “Install” button.
This will start the installation process.
Once the installation is complete, we can put the environment into operation. To do this, we check the “Run IntelliJ IDEA” box in the final window Community Edition” and press the “Finish” button.
Setting up the first Kotlin project
Let's start IntelliJ IDEA. The program start window will open. We select the “New Project” item there.
A window for creating a new project will then open.
In the “Name” field we enter the name of the project. Let’s call the project “KotlinTutorial_io”.
In the “Location” field you can specify the path to the project if the standard path is not desired.
Since we will be working with the Kotlin language, we select the “Kotlin” option in the “Language” field.
You can also specify the path to the Java SDK to be used in the project in the “JDK” field. Typically, by default, this field already contains the path to the JDK installed on the local computer. If this field is empty, it must be installed.
After that we click on the “Create” button.
IntelliJ IDEA will then create the project and open it.
In the left sidebar we can see the project structure. The “src” folder contains all files with the source code. By default, this consists of two subfolders: “main” for the program code and “tests” for test scripts. In the “main” folder, another folder called “kotlin” is usually created for files with Kotlin code. Initially, this folder is empty because our project does not yet have any code files.
To add a file with source code, we right-click on the path “src/main/kotlin” and select “New -> Kotlin Class/File” from the menu that pops up.
A small window will then open in which we must enter the name of the file. Let's call the file "main" (In a Kotlin project, the startup file is usually called main.kt).
After pressing Enter, a new Kotlin source code file is added to the “src” folder (in our example, the “main.kt” file). In the middle of the screen appears its content, the source code, which is blank by default. So let's add the following code:
The entry point into a Kotlin program is the main-Function. The keyword is used to define this function , followed by the name of the function, i.e main. This function does not take any parameters, which is why there are empty brackets after the function name.
The actions are defined within the curly brackets mainfunction executes. In this case the leads main-Function another function, viz println(), which prints a message to the console.
We are now finished with our first Kotlin program. To run this simple program, simply click the Run button in the top bar of the window.
After that, the project is created and the compiled program is executed in the console of IntelliJ IDEA.