Thuta Learning
Kotlin
BasicProgrammingBeginner

Kotlin HOME

KotlinLesson 115 min

What you'll walk away with

  • Say in two sentences what Kotlin is and where it runs (JVM, Android, back ends, multiplatform)
  • Name the five chapters of the course and what each one is for
  • Read a first Kotlin program that uses `fun main()`, `val`, a string template and `println`
  • Follow the study routine: predict the output, run it, change one thing, then break it on purpose

Explain it simply

Kotlin in plain words

Think of Kotlin as a strict but helpful editor for your instructions. You write what you want the computer to do in short, readable lines, and before anything runs the Kotlin compiler checks them: is a value of the wrong type being used, does a name exist, might a value be missing? Only code that passes the check becomes a running program.

The same language is used for phone apps, web back ends, command-line tools and code shared between platforms. You learn the basics once, and what this course teaches applies to all of them.

Problem → Why → Solution

The problem: starting without a map

Search for Kotlin tutorials and the first results talk about Android layouts, Gradle plugins, coroutines and Spring controllers all at once. A beginner who dives into one of them meets about ten unfamiliar ideas on the first screen, cannot tell which tool an error message comes from, and quits believing that Kotlin itself is hard. The language is not the hard part; the missing order is.

Why order matters

Every later chapter reuses the earlier ones. Null safety assumes you know what a type is, lambdas assume you can write a function, and coroutines assume both. Seeing the map of the course first gives each new idea a place to attach to.

Core concept: one language, a helpful compiler, many targets

Kotlin is a statically typed, general-purpose programming language created by JetBrains, the company behind IntelliJ IDEA. "Statically typed" means every value has a type that is known when the code is compiled: `Int` for whole numbers, `String` for text, `Boolean` for true or false. Because the compiler knows the types, it can reject a line that adds a number to a piece of text before the program ever starts. The language is open source and its syntax is compact, so things that need several lines of boilerplate in older languages are often a single line in Kotlin.

Kotlin source files end in `.kt`. The compiler, `kotlinc`, turns them into JVM bytecode, the same format that Java programs compile to. That is why a Kotlin program runs wherever there is a Java Virtual Machine, can call any Java library, and can be called from Java code in the same project. The same language can also be compiled for Android, for JavaScript and to native machine code, and Kotlin Multiplatform lets one codebase share logic across several of these targets.

1. Write

You save your instructions in a file such as `Welcome.kt`. A good editor already underlines problems in red while you type.

2. Check

`kotlinc` reads the file and checks every name and type. A typo or a mismatched type stops the build right here and points at the file, line and column.

3. Compile

When the checks pass, the compiler writes bytecode: class files, usually bundled into one jar.

4. Run

The JVM finds `main` and runs the bytecode from top to bottom, and the text from `println` appears in your terminal.

A first look at the language shows a few habits that differ from many beginner languages. A statement ends at the line break, so semicolons are optional. Values are declared with `val` (a name that never changes) or `var` (a name you may reassign). Text can include values directly through string templates such as `$name`. And the compiler tracks whether a value might be missing, which is called null safety and gets its own lessons later in this chapter.

This course is written for Kotlin 2.x. Every runnable example is one file with a top-level `fun main()`, so you can paste it into the Kotlin Playground or compile it yourself, as the Get Started lesson shows. You do not need Android Studio, a framework or any earlier Kotlin knowledge to follow along.

Mental model: write, check, compile, run
  1. Write Kotlin in a .kt file
  2. kotlinc checks types and names
  3. Bytecode is produced
  4. The JVM runs main()

The compiler is the strict first reader of your code. Most beginner mistakes are caught at step two, before anything runs.

General pattern: a tiny Kotlin program

kotlin
fun main() {
    val name = "some text"
    println("Text that mentions $name")
}

The lines between the braces of `main` run from top to bottom, one at a time. `val` ties a name to a value, and `println` prints text. No semicolon is needed at the end of a line.

Your first Kotlin program

This program stores two facts in named values and uses them to build three lines of output. After you run it, change `finished` to another number and run it again. If you can explain why the output changed, this lesson has done its job.

Welcome.kt
fun main() {
    val course = "Kotlin"
    val lessons = 33
    val finished = 1

    println("Welcome to the $course course!")
    println("You have finished $finished of $lessons lessons.")
    println("Lessons left: ${lessons - finished}")
}

Code breakdown

  1. The entry pointfun main() {

    `fun` declares a function, and the function named `main` is where the program starts. The braces `{ }` fence in the lines that will run.

  2. A text valueval course = "Kotlin"

    `val` ties the name `course` to `"Kotlin"`. Text goes in double quotes, and Kotlin works out by itself that the type is `String`; you do not have to write it.

  3. A whole numberval lessons = 33

    Whole numbers have the type `Int`. There is no semicolon here either: the end of the line ends the statement.

  4. Inserting a value into textprintln("Welcome to the $course course!")

    `println` prints the text and then starts a new line. Inside the text, `$course` is replaced by the value of `course`, which is Kotlin.

  5. An expression inside a templateprintln("Lessons left: ${lessons - finished}")

    To insert something more than a plain name, wrap it in `${ }`. Kotlin calculates `lessons - finished` and puts 32 into the text.

You should see
Welcome to the Kotlin course!
You have finished 1 of 33 lessons.
Lessons left: 32
Your path through the course
  1. Basic: syntax, types, control flow, null safety
  2. Intermediate: functions, collections, classes
  3. Advanced: lambdas, generics, coroutines, tooling
  4. Projects and Exercises: build and practise

How the 33 lessons are organized

The course has five parts, and each one builds on the part before it. Go through them in order, because the lambdas and coroutines in Advanced combine things you learn in Basic and Intermediate.

PartWhat it covers
Basic (10)Home, intro, setup, syntax, val and var, data types, if and when, loops, null safety, safe calls and the Elvis operator
Intermediate (8)Functions, lists, maps, classes and objects, constructors, inheritance, interfaces, data classes
Advanced (10)Lambdas, higher-order functions, generics and extensions, sealed types and Result, a mini project, coroutines, Flow, Gradle, testing, Multiplatform
Projects (3)A Task Manager built in three parts: setup, features and polish
Exercises (2)One practice set for fundamentals and one for OOP and collections

How to work through a lesson

Every lesson follows the same order: a plain explanation, a problem, the core concept, the general pattern, a worked example explained line by line, common mistakes, a mini challenge and three quiz questions. Reading alone is not enough. Kotlin's compiler messages are excellent teachers, and you only remember them after you have caused them yourself.

1. Predict

Before you run an example, write down what you expect it to print.

2. Run

Type it out and run it, in the Playground or on your own machine. Typing makes your eyes, hands and mind all work, which copying does not.

3. Change one thing

Change a value, a name or a type, and predict the new output before running again.

4. Break it on purpose

Misspell a name and read the compiler message. Once you can read the file, line and kind of error, fixing real mistakes later becomes much faster.

In real projects

Where Kotlin shows up in real projects

Android supports Kotlin as a first-class language, and a great many new apps are written in it. On the server, frameworks such as Ktor and Spring Boot can be used from Kotlin, and every Java library is already available, so nothing has to be rewritten. Gradle build files can be written in Kotlin too, as `build.gradle.kts`, which means the script that builds a project is often Kotlin itself.

Kotlin Multiplatform is used to share business logic such as validation or networking between more than one platform. Small command-line tools and data-processing scripts suit Kotlin as well, because the code is short and the compiler catches mistakes early. The basics you learn in this course are identical in all of those places; Android layouts and Spring annotations are layers placed on top of the language.

Common mistakes

Common beginner mistakes

1. Thinking Kotlin is only for Android

If you start with an Android tutorial, you cannot tell what the Android SDK provides and what the Kotlin language provides. Study the first chapters of this course in plain files that contain only `fun main()`, and pick a framework afterwards.

2. Reading examples without running them

If you guess the output but never run it, you cannot know whether you were right. Type each example and run it, then misspell `println` as `printn` or delete a closing bracket, and read the message you get. You recognise a compiler message the next time only if you have seen it once.

3. Jumping ahead to coroutines and lambdas

Later chapters assume you are comfortable with `val`, types, null safety and functions. Skip ahead before those are solid and every error message becomes harder to read, and going back costs more time than staying in order would have.

Best practices

Good habits from day one

  • Keep one scratch file for your own experiments from each lesson.
  • Whenever a compiler error appears, note the first line of the message; over time it becomes your own list of mistakes.
  • Do not ignore warnings either, because the compiler is often pointing at a better way to write the line.

Mini challenge

Mini challenge: a study planner

Write a program that works out how many days you need to finish the whole course at your own pace.

  • Store your name, the total number of lessons (33) and your lessons per day in three `val`s
  • Work out the number of days rounded up: at 4 a day, 33 lessons take 9 days, not 8
  • Print one sentence using string templates
  • Change `lessonsPerDay`, run again and check the result by hand
Planner.kt
fun main() {
    // TODO: three vals: your name, the total number of lessons, lessons per day
    // TODO: days needed, rounded UP (whole-number division drops the remainder)
    // TODO: print one sentence that mentions the name and all three numbers
}

Expected: Aye needs 9 days for 33 lessons at 4 a day.

Show hint

Dividing one Int by another drops the remainder, so 33 / 4 is 8. To round up, add the divisor minus one before dividing: (33 + 3) / 4 = 9.

Show solution
Planner.kt
fun main() {
    val name = "Aye"
    val totalLessons = 33
    val lessonsPerDay = 4

    val days = (totalLessons + lessonsPerDay - 1) / lessonsPerDay

    println("$name needs $days days for $totalLessons lessons at $lessonsPerDay a day.")
}

Quick quiz 1 / 3

What does the Kotlin compiler do before your program starts running?

Quick quiz 2 / 3

What does `println("Left: ${33 - 1}")` print?

Quick quiz 3 / 3

Which statement about Kotlin is correct?

What to remember

  • Kotlin is a statically typed language from JetBrains that compiles to JVM bytecode and is used for Android, back ends and multiplatform code.
  • The compiler checks names and types before the code runs, so most beginner mistakes show up early.
  • The course has 33 lessons: Basic 10, Intermediate 8, Advanced 10, Projects 3 and Exercises 2, best studied in order.
  • Study every example with the routine: predict, type and run, change one thing, break it on purpose.

Kotlin releases — Kotlin

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Kotlin HOME | Thuta Learning