Kotlin. Practice. Creating a simple project

Kotlin. Practice. Creating a simple Kotlin project. The program “Hello world!” Getting Started with IntelliJ IDEA interface


Contents


Search other resources:

1. Creating the simplest project

The integrated development environment for programs in different languages IntelliJ IDEA from JetBrains allows you to create software in different languages: Java, Python, Scala, Kotlin, PHP, Java Script and others.

To create a new project in the Kotlin language, a separate window is called (Figure 1) in one of the cases:

  • when the system is launched for the first time and no new projects have been created yet;
  • when a previously created (active) project is closed.

IntelliJ IDEA. Window for creating a new project

Figure 1. Window for creating a new project

 

2. The New Project command

To define a new project in the IntelliJ IDEA system and create a new project in the Kotlin language, follow these steps (Figure 2):

  • in the left part of the window, activate the Projects tab;
  • select the New Project command in the upper part of the window (Figure 2). As a result, the system will open the following window with the choice of the language or design pattern.

IntelliJ IDEA. The New Project command from the Projects menu

Figure 2. The New Project command from the Projects menu

 

3. Template selection window in Kotlin language

After choosing the command for creating a new project (New Project), a window opens in which you need to specify the platform on the basis of which the new project will be built.

Figure 3 shows the choice of the Kotlin language and the Application template, which allows you to create Backend applications with Kotlin/JVM. According to the task, you can choose another project template (for example, for mobile applications, etc.).

It is also possible to choose:

  • the name of the project. The default name is untitled;
  • the folder in which the project will be located;
  • the type of automatic build system that provides automation of tasks solved by the programmer (Gradle Kotlin, Gradle Groovy, and others);
  • Java developer toolkit (library) (JDK) in the form of libraries of different versions.

IntelliJ IDEA. Kotlin. Choosing the type of application

Figure 3. Choosing the type of application in the Kotlin language. The Application template is selected

After selecting the Next button, the system will go to the next window.

 

4. Specifying additional options for creating a project

At this stage, the following project options are specified (Figure 4):

  • template (Console Application, Web server);
  • testing environment (JUnit4 is offered by default);
  • the version of the target Java virtual machine (JVM).

Kotlin. Window for setting additional project options

Figure 4. Window for setting additional project options

In our case, you can change the template to Console Application as shown in Figure 5. This template is handy for learning the basics of the Kotlin language. You can confirm the required options by clicking the Finish button.

Kotlin. IntelliJ IDEA. Installing the Console Application template

Figure 5. Installing the Console Application template (Console Application)

After clicking the Finish button, a new console application will be created and the system will complete the process of creating a new application.

 

5. The “Hello World!” program window

At this point, the Console Application has been created and the program window looks something like the one shown in Figure 6. It is important to find the module (file) main.kt here. It is assumed that files in the Kotlin language have the *.kt extension. The main.kt module contains the main function of the program – main(), which is the entry point into the program. The program can contain any number of modules (files).

Kotlin. Console Applicaiton. The main.kt module. The program "Hello World!"

Figure 6. Console Applicaiton. The main.kt module. The program “Hello World!”

By default, the system offers a program code that displays the classic “Hello world!”

fun main(args: Array<String>) {
  println("Hello World!")
}

 

6. Start a project for execution

The created project is launched in one of the following ways:

  • by selecting the Run… command from the Run menu (Figure 7);
  • by pressing the Alt + Shift + F10 key combination.

IntelliJ IDEA. Kotlin. The command to start the project for execution

Figure 7. The command to start the project for execution

If the program is started for the first time, the next window will open as shown in Figure 8. A module with the main() function is set here. In our case, this is the main.kt module.

IntelliJ IDEA. Kotlin. The window for specifying the module that you want to run

Figure 8. The window for specifying the module that you want to run

 

7. Results. Further launches of the project (program)

The result of the program execution is displayed in the lower part as shown in Figure 9. Now you can change the program code, solve different tasks. Further execution of the program will be more simplified and accessible. In Figure 9, arrows show how to run our program.

Kotlin. The IntelliJ IDEA window after launching a project for execution

Figure 9. The IntelliJ IDEA window after launching a project for execution

 


Related topics