CodeBlanks Logo Icon CodeBlanks

Your first program

Learn to write your first Kotlin program with fun main and println.

Your first program: fun main and println

The heart of a Kotlin program is the fun main() function. The fun keyword declares a function; the name main is the entry point. To write text to the screen you use println(...).

You put a string between the parentheses, written between double quotes: "Hello". Kotlin does not require semicolons at the end of statements.

fun main() {
    println("Hello Kotlin")
}

Practice Write your first program

Write a fun main(). Print Hello Kotlin with println.

Sample solution

Main.kt
fun main() {
    println("Hello Kotlin")
}

Expected output

Hello Kotlin

Compile and run

Kotlin source code (a .kt file) is first compiled into JVM bytecode, then run on the JVM. From the command line you compile with kotlinc Hello.kt -include-runtime -d Hello.jar and run with java -jar Hello.jar.

IntelliJ IDEA and Android Studio do these steps automatically; you just write the code.

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which is a program's entry point?

  • fun main Correct answer
  • println
  • String
  • JVM

Explanation: fun main is the entry point of a Kotlin program.

2 Your first program: fill in the missing part

Question
fun ____() { println("Hello") }
Answer
fun main() { println("Hello") }

Explanation: fun main is the entry point.

3 What does println do?

  • Declares a variable
  • Writes a line to the screen Correct answer
  • Declares a class
  • Declares a function

Explanation: println writes a line to the screen and moves to a new line.

All lessons in Beginner