Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 134 ยท Getting Started

Introduction to Kotlin

What Kotlin is, where it runs (JVM, Android, servers, multiplatform), where it came from and why teams pick it. Includes four runnable examples.

Kotlin is a modern, statically typed programming language. You write short, readable code, the compiler checks it carefully before it runs, and the result works on the Java Virtual Machine (JVM), on Android phones, in browsers and in native apps. It was designed to be a pragmatic language: it does not try to prove a new theory of programming, it tries to remove the everyday friction that makes other languages tiresome.

This lesson is a tour. You will not learn every rule yet, but by the end you will know what Kotlin is, where people use it, how it looks, and why so many teams choose it. Every example here runs on this page, so press Run and change things as you read.

What is Kotlin?

Kotlin is a general-purpose language created by the company JetBrains (the makers of IntelliJ IDEA). The first public version appeared in 2011 and version 1.0 was released in 2016. In 2017 Google announced official support for Kotlin on Android, and in 2019 Google declared Android "Kotlin-first": new Android APIs and documentation are written with Kotlin in mind. The language is now developed in the open by JetBrains together with a foundation, and it is free to use. This tutorial teaches Kotlin 2.x, which uses the second-generation compiler called K2.

Three words describe Kotlin well:

  • Concise. Things that need ten lines in older languages often need one. A class that only carries data is a single line.
  • Safe. The type system separates values that can be null from values that cannot, so the infamous "null pointer" crash becomes a compile-time message instead of a production incident.
  • Interoperable. Kotlin compiles to the same bytecode as Java. It can call any Java library, and Java code can call Kotlin. You do not have to rewrite anything to start.

Your first taste

Here is a complete program. Do not worry about every symbol; just notice how little ceremony there is.

Kotlin Hello from Kotlin

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.

fun declares a function, main is the function Kotlin starts with, val creates a named value that cannot be reassigned, and println prints a line. Inside the quotes, $language inserts the value of the variable. There is no class around main, no public static void, and no semicolons. The next lesson explains each part.

Where Kotlin runs

Kotlin is one language with several targets. The code you write is mostly the same; the compiler turns it into whatever the target needs.

Target What it produces Typical use
Kotlin/JVM Java bytecode (.class files, .jar archives) Servers, tools, desktop, libraries, Android
Kotlin/JS JavaScript Web front ends
Kotlin/Wasm WebAssembly Browser apps that need speed
Kotlin/Native A native executable (no JVM) iOS, desktop, embedded
Kotlin Multiplatform Several of the above from one code base Sharing logic between Android, iOS and the web

This tutorial focuses on Kotlin/JVM, which is the most common target and the one our in-browser runner uses. Almost everything you learn applies to the other targets as well, because the language and the standard library are shared.

Our runner compiles your code with the real Kotlin compiler on a server and runs it on a recent JDK. What you see in the output is exactly what you would see on your own machine.

Why teams choose Kotlin

Less boilerplate

Many programs need small classes that just hold values: a customer, a point, a message. In Java 8-style code such a class needs fields, a constructor, getters, equals, hashCode and toString. In Kotlin it is one line, and the compiler writes the rest.

Kotlin A whole class in one line

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.

You will meet data class properly in Data Classes. For now the point is the ratio: one line of declaration, three useful behaviors.

Null safety built into the type system

A reference that may be missing is a different type from one that is always present. String can never be null; String? can. The compiler refuses to let you use a possibly missing value as if it were present.

Kotlin Nullable and non-nullable 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 symbols ?., ?: and let are covered in Null Safety. The key idea: the possibility of null is written in the type, so you cannot forget it.

Expressive standard library

Working with lists, maps and text is a daily task, and Kotlin's standard library makes it short and readable. You describe what you want, not how to loop for it.

Kotlin From a list to a report

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.

Smooth coexistence with Java

Because Kotlin and Java share the JVM, a team can add Kotlin to an existing Java project one file at a time. The Kotlin code can use every Java class, such as java.time.LocalDate or java.util.UUID, directly.

Kotlin Using a Java class from Kotlin

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 a whole lesson about this later: Java Interoperability.

Static typing without the typing

Kotlin is statically typed: every value has a type known at compile time, and mistakes such as adding a number to a list are caught before the program runs. But you rarely have to write the types. The compiler infers them from the values.

Kotlin
val count = 3          // Int
val price = 9.99       // Double
val title = "Kotlin"   // String
val tags = listOf("jvm", "android")   // List<String>

Languages like JavaScript or Python do not check types until the code runs, which makes small scripts quick to write but large programs risky to change. Older statically typed languages catch more mistakes but make you repeat yourself. Kotlin aims for both: the safety of static types and the lightness of a scripting language.

How Kotlin compares

A quick, honest comparison for people coming from other languages:

  • Versus Java: Kotlin has less boilerplate, built-in null safety, extension functions, when expressions, default arguments, and first-class coroutines. Java has a larger ecosystem of tutorials and has been adopting some of the same ideas (records, pattern matching). The two work together, so the choice is not all-or-nothing. See Kotlin for Java Developers.
  • Versus JavaScript or Python: Kotlin is checked by the compiler and usually runs faster, but you must compile it and tools matter more. In return, refactoring a big code base is far safer.
  • Versus Swift: The two languages are surprisingly similar in feel (null safety, data holders, closures). Swift targets Apple platforms first; Kotlin targets the JVM first and shares code with iOS through Multiplatform.
  • Versus Scala: Kotlin is deliberately smaller and simpler, with faster compile times and fewer ways to do the same thing.

When to use Kotlin, and when not to

Kotlin is an excellent choice for Android apps (it is the recommended language), back-end services (with frameworks such as Spring or Ktor), command-line tools, libraries that must be used from the JVM, and cross-platform logic shared between mobile apps.

It is a weaker choice when you need a tiny script that starts in a millisecond (the JVM takes time to warm up), when you must run on a platform with no Kotlin target, or when your team and tools are already deeply invested in another language and gain nothing from switching. A language is a means to an end; pick the one that makes your project succeed.

What you will learn in this tutorial

The tutorial is arranged so each lesson builds on the previous ones:

  1. Getting started: variables, types, numbers, strings, operators and input.
  2. Control flow and functions: if, when, loops, null safety, lambdas, extension functions.
  3. Collections and classes: lists, sets, maps, sequences, data classes, sealed types, objects, delegation.
  4. Generics and idioms: variance, reified types, DSLs, annotations.
  5. Real-world topics: errors and resources, text, files, dates, coroutines and flows, testing and tooling.
  6. Projects and practice: a calculator, a bank, a JSON parser and more.

You do not need to know another language first. If you can follow a recipe step by step, you can follow this tutorial. If you have programmed before, you will recognize many ideas and can move quickly.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which company created Kotlin?

    Kotlin was created by JetBrains, the company behind IntelliJ IDEA. Google later made it an officially supported (and then preferred) language for Android.
  2. 2. Which statement about Kotlin and Java is true?

    Kotlin/JVM produces ordinary bytecode, so Kotlin and Java classes can call each other in the same project.
  3. 3. What is the type difference between String and String? in Kotlin?

    A question mark after a type makes it nullable. The compiler then forces you to handle the missing-value case before using it.
  4. 4. What does the compiler do when you write val count = 3 without a type?

    Kotlin is statically typed, but it infers types from the value on the right, so you usually do not have to write them.
  5. 5. Which of these is NOT a Kotlin compilation target?

    Kotlin targets the JVM, JavaScript, WebAssembly and native code. There is no PHP target.

Summary

  • Kotlin is a concise, statically typed language from JetBrains, first released in 2011 with 1.0 in 2016.
  • It compiles to JVM bytecode, JavaScript, WebAssembly or native code, and Multiplatform lets you share code between them.
  • Its main strengths are less boilerplate, null safety in the type system, a rich standard library and full interoperability with Java.
  • Types are checked at compile time but usually inferred, so you rarely write them.
  • It is the preferred language for Android and a strong choice for servers and tools.

Next: Your First Kotlin Program