Kotlin Introduction

Learn what Kotlin is used for, how to run Kotlin code, and write your first program with main and println.

Lesson 1 of 43Beginner17 min read

Kotlin is a modern programming language created by JetBrains, the company behind the IntelliJ IDEA editor. In 2019 Google made it the preferred language for Android apps, and today most new Android apps are written in it. Kotlin is also used to build web servers, desktop tools and apps that share code between Android and iOS.

Kotlin runs on the Java Virtual Machine (JVM), the same engine that runs Java, so it can use every Java library ever written. But it was designed to fix many of Java's frustrations: code is shorter, null errors are caught before the program runs, and common tasks need far less typing.

In this lesson you'll learn where to run Kotlin, write your first program, print text to the screen and add comments. You don't need to know any other language first, though if you've used Java, Kotlin will feel familiar.

Where to write and run Kotlin

You have three easy options:

  • Kotlin Playground (play.kotlinlang.org): runs Kotlin in your browser with nothing to install. Perfect for this course's examples.
  • IntelliJ IDEA Community Edition: JetBrains' editor, with Kotlin built in. Choose New Project, pick Kotlin, and click the green run arrow next to main.
  • Android Studio: based on IntelliJ, used for Android apps.

Kotlin source files end in .kt. Behind the scenes the Kotlin compiler turns your code into JVM bytecode, which the Java runtime then runs. You don't need to manage those steps yourself; the run button does it for you.

The main function

Every Kotlin program starts in a function called main:

Kotlin
fun main() {
    println("Hello, Kotlin!")
}
  • fun declares a function, and main is its name.
  • The empty brackets () mean it takes no input.
  • The curly braces { } hold the code that runs, one statement per line.
  • Unlike Java, there's no class around it and no semicolons at the end of lines. Kotlin lets you leave them out.

println, print and comments

println prints its value and then moves to a new line. print prints without the new line, so the next output continues on the same line:

Kotlin
print("Ready, ")
print("steady, ")
println("go!")

Comments are notes for people that the compiler ignores. // starts a comment that runs to the end of the line, and /* ... */ can span several lines. Use them to explain why code does something, not to repeat what it obviously does.

How Kotlin runs: the JVM, Android and beyond

Kotlin code is compiled before it runs. The most common target is the Java Virtual Machine (JVM): the Kotlin compiler turns your .kt files into the same bytecode that Java produces, so Kotlin programs run anywhere Java does, on servers, desktops and Android phones.

Because both languages share the JVM, Kotlin and Java work together in the same project. A Kotlin file can call any Java library, and Java code can call Kotlin. That's why companies could adopt Kotlin gradually, one file at a time, instead of rewriting everything.

The same language also compiles to other targets:

TargetUsed for
Kotlin/JVMAndroid apps, backend services with Spring or Ktor, desktop tools
Kotlin/JS and Kotlin/WasmCode that runs in web browsers
Kotlin/NativePrograms that run without a JVM, such as iOS code
Kotlin MultiplatformSharing business logic between Android, iOS, web and desktop

This course focuses on the language itself, which is the same on every target, and runs its examples on the JVM.

Kotlin Basics Syntax

A Kotlin program

fun main() {
    // a one-line comment
    println("Text on its own line")
    print("No new line after this")
}

Kotlin Basics Summary

  • Kotlin is a modern JVM language used for Android, servers and multiplatform apps.
  • Programs start in fun main(); files end in .kt.
  • println prints and adds a new line; print doesn't.
  • Semicolons are optional, and main doesn't need a class around it.
  • // and /* */ write comments the compiler ignores.

Kotlin Basics Examples

Each example shows the code, the output it produces and what each part does.

Example 1

Your first program

Kotlin
fun main() {
    println("Hello, Kotlin!")
    println("I'm learning to code.")
}
Output
Hello, Kotlin!
I'm learning to code.
Line-by-line explanation
  1. Line 1Every program starts in main.
  2. Lines 2 to 3Each println prints its text on a new line.
Example 2

print versus println

Kotlin
fun main() {
    print("Loading")
    print(".")
    print(".")
    println(".")
    println("Done!")
}
Output
Loading...
Done!
Line-by-line explanation
  1. Lines 2 to 4print keeps the output on the same line.
  2. Line 5println finishes the line, so Done! starts on the next one.
Example 3

Numbers and comments

Kotlin
fun main() {
    // Kotlin can calculate as well as print text
    println(7 + 5)
    println("7 + 5")
    /* The first line prints the result of the sum.
       The second prints the characters exactly as written. */
    println(10 * 3)
}
Output
12
7 + 5
30
Line-by-line explanation
  1. Line 3Without quotes, Kotlin calculates the sum and prints 12.
  2. Line 4Inside quotes it's just text, printed exactly as written.
  3. Lines 5 to 6A multi-line comment is ignored when the program runs.

Common Kotlin Basics Mistakes

Capital letters in println or main

Don't
fun Main() {
    Println("Hi")
}
Do
fun main() {
    println("Hi")
}

Kotlin is case-sensitive: main and println must be lowercase.

Forgetting the quotes around text

Don't
println(Hello)        // error: Unresolved reference: Hello
Do
println("Hello")

Code outside main

Don't
println("Hi")
fun main() { }
Do
fun main() {
    println("Hi")
}

Statements like println must be inside a function.

Kotlin Basics in Real Projects

Kotlin in practice

Apps such as Google Maps, Pinterest, Trello and Duolingo use Kotlin on Android. Companies including Netflix, Atlassian and Amazon use it on their servers, often with frameworks like Spring Boot and Ktor. Kotlin Multiplatform lets teams write business logic once and share it between Android, iOS and desktop apps.

Kotlin Basics FAQ

Is Kotlin better than Java?

Kotlin was designed to fix common pain points in Java: it needs less boilerplate code, builds null safety into the type system and adds features such as data classes and extension functions. Java remains excellent and widely used, and the two work together in one project. For new Android apps, Google recommends Kotlin.

Do I need to learn Java before Kotlin?

No. This course assumes no programming experience. Knowing Java helps when you read older Android code or use Java libraries, but you can learn Kotlin first and pick up the Java you need later.

Is Kotlin only for Android?

No. Android is the best-known use, but Kotlin is also used for backend services with Spring Boot and Ktor, command-line tools, data processing and, through Kotlin Multiplatform, sharing code between Android, iOS, web and desktop apps.

Which Kotlin version should I learn?

Learn a current Kotlin 2 release. Kotlin 2.0 introduced the faster K2 compiler, and the language you write is almost the same as in late Kotlin 1.x versions, so tutorials from the last few years still apply.

Is Kotlin free to use?

Yes. Kotlin is open source under the Apache 2.0 licence and free for any use, including commercial apps. IntelliJ IDEA Community Edition and Android Studio, the main editors for Kotlin, are free too.

Kotlin Basics Exercises

Answer each one and check it instantly. If you get stuck, open the hint before the answer.

Fill in the blankseasy

Complete the program so it prints Hi.

 main() {
    ("Hi")
}
Predict the outputeasy

What does this print?

fun main() {
    print("A")
    print("B")
    println("C")
}
Predict the outputmedium

What does this print?

fun main() {
    println("2 + 2")
    println(2 + 2)
}
Multiple choicemedium

Which file extension do Kotlin source files use?

Multiple choicehard

Why can Kotlin programs use Java libraries?

Kotlin Basics Quiz

5 questions to check what you learned.

  1. Where does every Kotlin program start?

    • In a class called Program
    • On the first line of the file
    • In a function called start
    • In the main function
  2. Which company created Kotlin?

    • Google
    • JetBrains
    • Oracle
    • Microsoft
  3. Are semicolons required at the end of Kotlin lines?

    • No, they're optional
    • Yes, always
    • Only inside main
    • Only after println
  4. What does // start?

    • A division
    • A string
    • A one-line comment
    • A new function
  5. What's the difference between print and println?

    • println is faster
    • print only works with numbers
    • println moves to a new line after printing; print doesn't
    • There's none