Cookie Preferences

We use cookies to enhance your experience. Choose your preference for cookie usage.

Essential cookies are required for basic functionality. Additional cookies help us improve our service and provide analytics.

View third-party services
  • Google Analytics: Website traffic analysis and conversion tracking
  • RudderStack: Analysis of user interactions with the website
  • Microsoft Clarity: User behavior analysis & session recordings
  • Facebook Pixel: Marketing analysis of user activity
KotlinThe first look at Kotlin

The first program

8 seconds read

In this lesson, we will take a look at your first Kotlin program, which prints "Hello, World!" This program is traditionally used as the first program to introduce beginner programmers to a programming language. Despite the fact that the program is very simple, it is still a working program and shows the basic syntax of a programming language.

The Hello World program

Here is the source code of this program:


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

To start the program, click the triangle on the right. It outputs "Hello, World".

If you have already installed the programming environment on your computer, you may run the program locally. If not, do not install it right now. We will learn it further.

The basic terminology

After you have seen the result, let's learn the basic terminology and then try to understand this program.

  • program a sequence of instructions (called statements), which are executed one after another in a predictable manner. A sequential flow is the most common and straightforward, where statements are executed in the order that they are written, from top to bottom one after the other;
  • statement (or programming statement) — a single action (like printing a text);
  • block — a group of zero or more statements enclosed by a pair of braces {...}; the program above has one block;
  • keyword — a word that has a special meaning in the programming language. Names of keywords are always the same;
  • identifier or name is a word written by a programmer to identify something;
  • comment — a piece of text that is ignored when executing the program, but can help explain a part of the program. It starts with //;
  • whitespace — a blank, tab, or newline; they are used to separate words in the program and improve readability.

The Hello World program under a microscope

The Hello World program illustrates the basic elements of any Kotlin program. Right now, we will consider only the most important of them.

The entry point. The keyword fun defines a function that contains code to be executed. This function has the special name main. It indicates the entry point for a Kotlin program. The function has a body enclosed inside braces {...}.

fun main(args: Array<String>) { 
    // ...
}

We will discuss the functions later. The name of this function should always be the same: (main). If you name it Main or MAIN or something else, the program will not start.

Note: The text after // is just a comment, not a part of the program. We will learn about comments in another lesson.

Printing "Hello, World!" The body of this function consists of programming statements which determine what the program should do after starting. Our program prints the string "Hello, World!" using the following statement:

println("Hello, World!")

This is one of the most important things to understand about the Hello World program. We invoke the special function println to display the string followed by a new line on the screen. We will often use the same approach to print something.

Remember that "Hello, World!" is not a keyword or a name, it is just a string literal to be printed on the screen.

Programs with multiple statements

As a rule, a program contains multiple statements. Each statement must start from a new line. As an example, the following program has two statements.

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

It displays:

Hello
World

Conclusion

We have considered the simplest program, which prints "Hello, World!" It has one function named main which represents the entry point for this program. Don't worry about all those terms (syntax, statement, block) just yet. We will explain them throughout this course. Don’t forget to use the provided program as a template for your own programs.

How did you like the theory?
Report a typo