Skip to content

Kotlin Basics

Glossary

144 terms from Kotlin Basics, in plain words, each with the chapter that teaches it.

#

!! (non-null assertion)
The `!!` operator tells Kotlin to treat a nullable value as non-null. If the value is actually null, the program crashes with a `NullPointerException`, so `?.`, `?:` or an `if` check is usually safer.
?. (safe call)
`?.` calls a function only if the value isn't null. If it is null, the whole result is null and nothing crashes.
?: (Elvis operator)
`?:` gives a fallback value when the expression on its left is null. For example, `phone ?: "no phone"` gives "no phone" when phone is null.

A

apply
A scope function that runs a lambda with the object as `this`, then gives the object back. It is handy for setting several properties of a new object, such as a Ticket.
argument
A value you pass to a function when you call it. In `lineTotal(3, 4.75)`, the values 3 and 4.75 are the arguments.
arithmetic operators
`+`, `-`, `*`, `/` and `%` (the remainder after dividing) do math, with `*` and `/` before `+` and `-`. `+=` changes a `var` by an amount and `++` adds 1.
array
A fixed-size collection of values, made with `arrayOf`. Kotlin lists are more flexible and are usually used instead.
assignment
Storing a value in a variable with a single `=`. The value on the right goes into the name on the left.
async
Starts a coroutine that runs side by side with others and gives back a Deferred that will hold its result. Use it when you need an answer back, such as a price.
await
Pauses a coroutine until a Deferred's result is ready, then gives you that result.

B

BigDecimal
A type that stores decimal numbers exactly. It is an option for money, because a `Double` can't store most decimals exactly.
block
The lines of code between `{` and `}`. An `if` runs its block only when its condition is true.
Boolean
A type with only two values, `true` and `false`. Comparisons such as `total >= 50` give a Boolean.
break
Leaves a loop at once. `while (true)` loops use it to stop when the user is done.
build tool
A program that fetches a project's dependencies, compiles all its files and runs the result. Gradle is a build tool commonly used for Kotlin projects.

C

camelCase
A way to write names with a capital letter for each word after the first, such as `cupsSold`.
case sensitive
Capital and small letters count as different. `println` works but `Println` is an error.
Char
A type holding a single character, written in single quotes, such as `'L'`.
check
Like `require`, but raises an IllegalStateException when the condition is false. Use it to check your program's own state.
class
A blueprint for your own type, describing the properties and methods its objects have. `Cafe` is a class, and `bloom` is one object made from it.
closed class / open
A normal class is closed, so nobody can extend it. Marking it `open` lets other classes build on it, and marking a function `open` lets a subclass override it.
collection
A value that holds many values in one variable. Kotlin's three main kinds are lists, sets and maps.
collection functions (filter, map, sumOf, etc.)
Functions on collections that take a lambda and apply it to each item. `filter` keeps matching items, `map` transforms each item, and `sumOf` totals them. They return new values and don't change the original.
command
One instruction typed into a terminal and run when you press Enter. In examples, a leading `$` stands for the prompt and isn't typed.
comment
A note for people, starting with `//` or placed between `/*` and `*/` to run over several lines. Kotlin ignores it.
comparison operators
Operators that compare two values and give a Boolean: `==` (equal; a single `=` assigns), `!=`, `<`, `>`, `<=` and `>=`.
compiler
A program that checks your Kotlin code and translates it into a form the computer can run. If it finds a mistake, it stops and tells you where.
continue
Skips the rest of the current pass of a loop and goes on to the next one.
copy
A data class function that makes a new object with some properties changed, using named arguments, such as `latte.copy(price = 4.25)`.
coroutine
A task that can pause while it waits, let other tasks run, and then carry on. It works like a barista who pulls an espresso while the milk steams.
coroutineScope
Runs its lambda and waits for every coroutine started inside it before it returns. This is how structured concurrency is done.
custom getter
A property written with `get()` that works out its value each time it is read, so it is always up to date. `needsReorder` is an example.

D

data class
A class for holding values. Putting `data` before `class` makes Kotlin write a readable `toString`, a value-comparing `==`, `copy` and destructuring for you.
data object
An `object` with `data` in front of it, such as `Cash`. It is a single shared instance that prints with its own name.
default argument
A parameter value that is used when the caller leaves that argument out, as in `shop: String = "Kettle & Crane"`.
Deferred
What `async` gives back: a value that will be ready later. `await()` pauses until it arrives.
delay
A suspend function that pauses a coroutine for a number of milliseconds without blocking the thread, so other coroutines keep running.
dependency
A library your project needs. Gradle's `dependencies` block lists them, such as `kotlinx-coroutines-core`.
destructuring
Unpacking an object's properties into separate variables in constructor order, such as `val (code, name, price) = latte`.
Double
A type for numbers with a decimal point, such as 55.0. It can't store most decimals exactly, so 0.1 + 0.2 gives 0.30000000000000004.
downTo / step
`downTo` makes a range count down, and `step` makes it jump by more than 1. For example, `10 downTo 0 step 5` visits 10, 5 and 0.

E

enum class
A type with a fixed set of named values, such as `Size.SMALL`, `Size.MEDIUM` and `Size.LARGE`. A typo in a value won't compile.
exception
An object describing a problem that happened while the program runs. If nothing handles it, the program stops; `try`/`catch` can handle it.
exhaustive
A `when` that covers every possible value, such as every enum value or every sealed subclass. It then needs no `else`, and the compiler complains if you add a case and forget a branch.
expression
Code that produces a value, such as `2 + 3`. `if`, `when` and `try` can all be used as expressions, and then they need to give a value in every case.
extension function
A function added to an existing type from outside it, such as `fun Double.money()`. You call it with a dot as if it belonged to the type.
extension property
A property added to an existing type from outside it, written with a custom getter, such as `val String.isMenuCode`.

F

for loop
A loop that runs once for each value in a range, list or string, putting the value in a variable you name.
format code
Symbols inside text used with `.format(...)` that say how to show a value. `%.2f` means a decimal with two digits after the point, and `%-12s` means a string padded to 12 characters, left-aligned.
function
A named block of instructions started with `fun`. A function call runs it, with any needed values inside the parentheses.
function type
The type of a function value, such as `(Double) -> Double`, meaning "takes a Double and gives back a Double".

G

generic
A type that has another type in angle brackets saying what it holds, such as `List<String>`, a list of strings.
Gradle
The usual build tool for Kotlin and Android. It reads build files such as `build.gradle.kts`, written in Kotlin, to fetch libraries, compile and run your project.
Gradle wrapper
Two small scripts in each project, `gradlew` (macOS/Linux) and `gradlew.bat` (Windows), that download the right Gradle version the first time they run. You don't have to install Gradle yourself.

H

Homebrew / SDKMAN
Tools that install software from a terminal. `brew install kotlin` (Homebrew, macOS) and `sdk install kotlin` (SDKMAN, macOS or Linux) install the command-line Kotlin compiler.

I

if / else
`if` runs a block only when its condition is true, `else if` adds another check, and `else` catches the rest. It can also give a value, and then it needs an `else`.
IllegalArgumentException / IllegalStateException
Exceptions raised by your own checks: `require` raises IllegalArgumentException for a bad value, and `check` raises IllegalStateException for a bad program state.
import
A line at the top of a file that lets it use names from a package, such as `import kotlinx.coroutines.*`.
in / !in
`in` checks whether a value is in a range or collection, and `!in` checks that it isn't. For example, `12 in 1..20` is `true`.
index
The position of a character in a string or an item in a list, starting at 0. So `drink[0]` is the first character.
inheritance
Building one class on another. A subclass such as `Latte` gets what its parent `Beverage` has and can replace open functions.
init block
A block in a class that runs whenever an object is made. It is a good place for `require` checks.
Int
The type for whole numbers up to about 2 billion. Bigger numbers need a `Long`.
integer division
Dividing an `Int` by an `Int`, which drops the decimals, so `7 / 2` is 3. Making either number a `Double` gives 3.5.
IntelliJ IDEA
A free editor made by JetBrains, the makers of Kotlin, used to write and run programs. It has Kotlin support built in.
interface
A list of functions that several classes promise to provide. A class that implements it must write each function with `override`, such as `price()` in `Priced`.
is
A keyword that checks a value's kind, as in `is Card`. After the check, Kotlin smart-casts the value to that kind.
it
The automatic name for a lambda's only parameter, so you don't have to name it. For example, `prices.filter { it < 4.0 }`.

J

jar
A packed file holding a compiled program that the JVM can run, such as `hello.jar`.
JDK
The Java Development Kit, which provides the JVM and its tools for building and running Java and Kotlin programs. Eclipse Temurin 21 is one free, long-term-support version.
Job
What `launch` gives back: a handle on a running coroutine. `job.join()` pauses until that coroutine has finished.
JVM
The Java Virtual Machine, which runs compiled Kotlin programs the same way on Windows, macOS and Linux.

K

key / value
The two parts of a map entry. You look up a value by its key, such as the price for the code `"lat"`.
Kotlin
A modern programming language made by JetBrains, used mainly for Android apps and servers. Its source files end in `.kt`.
kotlinc
The command-line Kotlin compiler, run in a terminal. For example, `kotlinc hello.kt -include-runtime -d hello.jar` compiles to a jar.
kotlinx-coroutines-core
The JetBrains library that provides coroutines, including `launch`, `async` and `delay`. A Gradle project adds it as a dependency.

L

lambda
A piece of code in braces that you can store in a variable or pass to a function. Its parameters come before `->` and its value is its last line.
launch
Starts a new coroutine and carries on at once without waiting for it. It gives back a Job.
let
A scope function that passes the object to a lambda as `it` and gives back the lambda's result. With `?.`, it runs only when the value isn't null.
library
A bundle of ready-made code for your programs to use. A library your project needs is a dependency.
list
A collection holding values in order, read by index. `listOf` makes a read-only list, and `mutableListOf` makes one you can add to and remove from.
logical operators
`&&` (and) is true when both sides are true, `||` (or) when at least one is, and `!` (not) flips a Boolean. `&&` checks its right side only when the left is true.
Long
A type for whole numbers bigger than an `Int` can hold. `delay` takes a Long.
loop
Code that repeats a block, such as a `for` or `while` loop.
LTS
Long-term support: a version that gets updates for a long time. JDK 21 is an LTS version.

M

main function
The function called `main`, where every program starts running. In IntelliJ you run it with the green arrow beside `fun main()`.
map
A collection that links each key to a value, such as menu codes to prices. `"lat" to 4.75` makes one entry, a pair, and looking up a missing key gives null.
Maven Central
A large free online collection where most Kotlin and Java libraries are published. Gradle's `mavenCentral()` tells it to look there.
measureTimeMillis
A function that runs a lambda and gives back how long it took in milliseconds. It needs `import kotlin.system.measureTimeMillis`.
method
A function written inside a class, which can use the object's properties directly. `label()` in `Cafe` is a method.

N

named argument
An argument written with its parameter's name and `=` before the value, such as `width = 30`. Named arguments can come in any order.
null
Kotlin's way of writing "no value". Only nullable types, written with a `?` such as `String?`, can hold null, and Kotlin makes you handle null before using such a value.
NullPointerException
The crash you get when `!!` meets a null value. Null safety is designed to prevent it.
NumberFormatException
The exception raised when `toInt()` is given text that isn't a number, such as `two`. `toIntOrNull()` avoids it.

O

object / instance
A value made from a class. `bloom` is one `Cafe` object, with its own property values.
overloading
Giving two functions the same name but different parameters. Kotlin picks the one that matches the arguments.
override
Replaces a function a class gets from elsewhere, such as `toString` or an interface's `price()`.

P

package
A named group of code, such as `kotlinx.coroutines`. You import names from a package to use them.
pair
A key linked to a value, made with `to`, as in `"lat" to 4.75`. `mapOf` takes pairs.
parameter
A name in a function's definition for a value it receives. In `fun lineTotal(quantity: Int, price: Double)`, `quantity` and `price` are parameters.
plugin (Gradle)
A Gradle feature added in the `plugins` block. `kotlin("jvm")` compiles Kotlin, and `application` adds a `run` task.
primary constructor
The parameters after a class's name, which are the values every new object needs. Putting `val` or `var` in front makes each one a property.
print / println
`println(...)` prints what's in the parentheses and then starts a new line, while `print(...)` prints without starting a new line.
private
Means only code inside the class can use that property or function, so details stay hidden.
program
A list of instructions for a computer, written as text called source code.
property
A variable that belongs to an object, read with a dot such as `bloom.name`. A `val` property can't change, and a `var` property can.

R

range
A run of values. `1..20` includes 20, while `1..<12` and `1 until 12` stop just before 12; `in` checks whether a value is in a range.
read-only vs mutable
A read-only value can't be changed once made: a `val` variable, a `listOf` list, or a string. A mutable one, such as a `var` or a `mutableListOf` list, can be changed.
readln / readlnOrNull
`readln()` waits for the user to type a line and gives back the text. `readlnOrNull()` does the same but gives null when there is no more input.
receiver
The type or value an extension function is called on. Inside the function it is `this`; in `fun String.initials()`, the receiver is a String.
repeat
A function that runs a lambda a set number of times, passing the count (from 0) as `it`.
require
Raises an IllegalArgumentException with your message if a condition is false. Use it to keep bad data out, such as a negative quantity.
return value
The value a function gives back with `return`, which takes the place of the call.
runBlocking
A bridge that starts the first coroutine, runs its lambda, and keeps `main` going until everything inside is done.

S

scope
The part of a program that can see a variable. A variable made inside a function ends at that function's closing brace.
scope functions
Functions such as `let` and `apply` (also `run`, `with` and `also`) that run a lambda on an object.
sealed class
A class whose subclasses are all known when the code is compiled. Each value is exactly one of that fixed set of kinds, such as a Card, Cash or GiftCard payment, so `when` can be checked for completeness.
set
A collection that holds each value only once, dropping duplicates.
single-expression function
A function whose body is one expression, written with `=` instead of braces and `return`. Kotlin works out the return type.
smart cast
Kotlin treating a value as a narrower type after a check. After `if (email != null)`, `email` is a plain String, and after `is Card`, `payment` is a Card.
source code
The text of a program, written in a language such as Kotlin and stored in files like `hello.kt`.
statement
One instruction in a program, written one per line in Kotlin with no semicolon.
string
Text in double quotes. Strings never change; each string function gives back a new string.
string functions
Functions called on a string with a dot, such as `trim()`, `uppercase()`, `contains(...)`, `replace(...)` and `substring(...)`; `length` is a fact, not a call. They give back a new string and leave the original unchanged.
string template
A way to put values inside text: `$name` inserts a variable and `${ }` inserts a calculation.
structured concurrency
The rule that coroutines live inside a scope that waits for all of them, so none is left running and forgotten.
subclass
A class that is a kind of another class, such as `Card : Payment()`. A `Card` can go wherever a `Payment` is expected.
suspend function
A function that may pause, marked with `suspend`. It can only be called from a coroutine or another suspend function.

T

task (Gradle)
A job Gradle can do, such as `run` or `build`.
terminal
A window where you type commands: PowerShell on Windows, the Terminal app on a Mac or Linux. IntelliJ has one built in.
this
The object a method (or an extension function) was called on. Inside a method, `name` is the same as `this.name`.
thread
A line of work the computer runs, doing one thing at a time. Many coroutines can share one thread, taking turns when one waits.
Thread.sleep
Java's pause, which blocks the whole thread so no other coroutine on it can run. In coroutines, use `delay` instead.
toInt / toDouble / toIntOrNull / toDoubleOrNull
Functions that convert text to numbers. `toInt()` crashes on bad text, while `toIntOrNull()` and `toDoubleOrNull()` give null instead. `toString()` turns a number into text.
top-level
Defined directly in a file, not inside a class or function. Any function in the project can use a top-level function or value.
toString
The function that gives the text shown when you print an object. You can `override` it, and a data class makes a readable one for you.
trailing lambda
A lambda that is the last argument and is written after the closing parenthesis, as in `applyRule(4.75) { it * 2 }`.
try / catch
`try` holds risky code and `catch` holds the fallback that runs if an exception happens. Like `if`, `try` can give a value.
type
The kind of a value, such as Int or String. Type inference means Kotlin works out the type from the value so you rarely write it.

U

Unit
The return type of a function that gives nothing useful back, such as one that only prints. `main` is one.

V

val / var
Keywords that make a variable. A `val` is read-only and can't be given a new value, while a `var` can change.
variable
A name that holds a value, made with `val` or `var`.

W

when
Checks one value against many branches, each written as a value, `->` and what to do. With no subject, each branch is a condition and the first true one wins.
while loop
A loop that repeats as long as its condition is true. Something inside must make it false eventually, or it runs forever.