Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 134 ยท Getting Started

Your First Kotlin Program

Write and run your first Kotlin program. Learn fun main, println vs print, statements, comments, main with arguments and the most common beginner errors.

Every programming journey starts with a program that prints something. In Kotlin that program is only two lines, and yet it already contains the skeleton of every Kotlin program you will ever write: a function called main, a call to another function, and some text. This lesson takes the first program apart piece by piece, shows what happens when you break it, and introduces comments, print versus println, and the form of main that receives command-line arguments.

The smallest program

Press Run on the example below. The output appears underneath the editor.

Kotlin Hello, world

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

Here is what each piece does:

  • fun is the keyword that declares a function, a named block of code you can run.
  • main is the function name. Kotlin looks for a function called main and starts the program there. It is the entry point.
  • () is the parameter list. The empty parentheses mean main needs nothing from the caller.
  • { ... } is the body: the statements that run when the function runs.
  • println("Hello, world!") calls the standard-library function println ("print line") with one argument, the text to print. The text sits between double quotes and is called a string literal.

Notice what is missing compared with many other languages. There is no class wrapped around main, no public static void, no import for println, and no semicolon at the end of the line. Kotlin imports a small set of very common functions automatically, and println is one of them.

println writes the text and then moves to a new line. print writes the text and stays on the same line, so the next output continues right after it.

Kotlin print versus println

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

The first four print calls build the single line Loading... done. Calling println() with no argument prints just a line break, which is the easiest way to get an empty line.

Both functions accept almost anything, not only strings. Numbers, booleans and many other values are converted to text for you.

Kotlin Printing values of different types

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

The expression 2 + 3 * 4 is calculated first (multiplication before addition, as in school mathematics) and the result 14 is printed. In the last line the + joins two pieces of text.

Statements and layout

A program is a list of statements, instructions that run one after the other, top to bottom. Each statement normally sits on its own line. Kotlin ends a statement at the end of the line, so you do not write semicolons. You may put several statements on one line separated by ;, but almost nobody does.

Kotlin Statements run in order

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

Indentation is not required by the compiler, but the Kotlin style is four spaces per level. Consistent indentation lets a reader see structure at a glance, and every editor can format it for you.

Comments

A comment is text for humans. The compiler ignores it. Kotlin has two kinds.

Kotlin
// A line comment runs to the end of the line.
val x = 1 // it can also follow code

/* A block comment can span
   several lines. */

/**
 * A documentation comment (KDoc) describes a function or class.
 * Tools turn it into help text and show it in your editor.
 */

Block comments can be nested in Kotlin, unlike in Java or C: /* outer /* inner */ still a comment */ is one complete comment. Good comments explain why something is done, not what the code already says. // add one to x above x + 1 is noise.

Kotlin Comments do nothing at run time

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

Commenting code out like this is a quick way to switch statements off while you experiment. In an editor you can do it with a keyboard shortcut (Ctrl+/ in IntelliJ IDEA).

Strings, in brief

Text is written between double quotes. A backslash starts an escape sequence for characters that are hard to type: \n is a new line, \t a tab, \" a quote inside a string and \\ a backslash. A dollar sign inserts a variable, which you will use constantly.

Kotlin Escapes and a first template

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

val name = "Ada" creates a named value (the lesson Variables covers it in depth), and $name places it in the text. Strings covers all of this fully.

main with arguments

You may see a second form of main:

Kotlin
fun main(args: Array<String>) {
    println("I received ${args.size} arguments")
}

When you run a program from a terminal you can pass words after its name, and they arrive in args. In a plain fun main() you simply do not receive them. Both forms are valid; use the short one unless you need the arguments. In our browser runner no arguments are passed, so args.size is always 0.

Kotlin main with args

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

There is also a version where main can suspend (suspend fun main()), which is part of coroutines. Ignore it for now.

The most common beginner mistakes

Compilers are strict, and your first errors are a rite of passage. Reading the message carefully is a skill worth building from day one. Kotlin messages have the form file:line:column: error: description.

Misspelling a function name

Kotlin Unresolved reference (fails on purpose)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

The compiler reports main.kt:2:5: error: unresolved reference 'printn'. It means "I cannot find anything called printn". Fix the spelling: println.

Forgetting a quote

Kotlin Unclosed string (fails on purpose)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

The compiler reports main.kt:2:20: error: syntax error: Expecting '"'. followed by a second error, Expecting ')'. It reached the end of the line while still inside the quotes. Add the missing ". When one mistake confuses the compiler, it often prints several follow-up errors. Always fix the first error first, then compile again.

Wrong case

Kotlin is case-sensitive: Println, PRINTLN and println are three different names, and only the last exists. The same is true for Main: the entry point must be spelled main.

A program without a valid main function can compile, but there is nothing to start, so it cannot run. Check the spelling and the case of main first.

Running a program for real

On the page, the Run button does everything for you. On your own computer the steps are the same, only separate. The next lesson, How Kotlin Code Runs, covers the compiler, bytecode and IDEs. For now remember the three stages:

  1. You write the source code (a .kt file).
  2. The compiler checks it and translates it into bytecode, and reports errors if it finds mistakes.
  3. The runtime executes the result and shows the output.

A slightly bigger first program

Now put several ideas together: a function other than main, a comment and printing in a loop. Read it first, guess the output, then run it.

Kotlin Calling your own function

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

greet is a function you wrote. It takes one parameter, name, whose type String is written after a colon. The order of declarations in a file does not matter: main can call greet even though greet is written above it, and it would work equally well if it were written below. The for loop repeats its body for each number from 1 to 3; loops have their own lesson, Loops.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the entry point of a Kotlin program?

    Execution begins in the function named main. Other functions run only when something calls them.
  2. 2. What is the difference between print and println?

    Both write text, but println moves to a new line afterwards, so the next output starts on a fresh line.
  3. 3. How do you end a statement in Kotlin?

    Kotlin ends a statement at the end of the line. Semicolons are only needed to put several statements on one line.
  4. 4. What does the compiler say about printn("x") when printn does not exist?

    The compiler cannot find a function with that name, so it fails with an "unresolved reference" error and never starts the program.
  5. 5. Which of these is a valid line comment?

    Kotlin uses // for line comments and /* ... */ for block comments.

Summary

  • A Kotlin program starts in fun main(); no class and no semicolons are required.
  • println prints a value and a line break, print stays on the same line, and println() alone prints an empty line.
  • Statements run top to bottom, one per line.
  • // starts a line comment and /* ... */ a block comment; comments should explain why, not what.
  • fun main(args: Array<String>) receives command-line arguments.
  • Compiler errors have the form file:line:column: error: message. Fix the first one first.

Next: How Kotlin Code Runs