A fast lookup for the Kotlin used in this book. Each entry notes the chapter where it was introduced.
Variables (Ch2)
var score = 0 // mutable — can change
val maxLives = 3 // read-only — fixed once set
var shipX: Float = 0f // explicit type when needed
Rule of thumb: use val unless you know it must change.
Basic Types (Ch2)
| Type | Holds | Example |
|---|---|---|
Int |
whole numbers | 42 |
Float |
decimals (note the f) |
3.5f |
Double |
higher-precision decimals | 3.5 |
Boolean |
true / false | true |
String |
text | "hello" |
Char |
one character | 'A' |
Type Conversion (Ch2)
val f = 1080.toFloat() // Int -> Float
val i = 342.7f.toInt() // Float -> Int (drops the decimal: 342)
Operators (Ch2, Ch4)
Arithmetic: + - * / % (modulo = remainder). Integer division drops the fraction (10 / 3 == 3).
Compound: += -= *= /=. Increment/decrement: ++ --.
Comparison: == != < > <= >=.
Logical: && (and), || (or), ! (not).
String Templates (Ch6)
val msg = "Score: $score, next: ${score + 10}"
Decisions (Ch4)
if (a > b) { ... } else if (a == b) { ... } else { ... }
// if and when can RETURN a value:
val biggest = if (a > b) a else b
val color = when (state) {
0 -> "red"
1 -> "green"
else -> "blue"
}
Loops & Ranges (Ch6)
while (running) { ... }
for (i in 0..10) { ... } // 0 to 10 inclusive
for (i in 0 until 10) { ... } // 0 to 9
for (i in 10 downTo 1) { ... } // counts down
for (i in 0..100 step 10) { ... }
for (item in collection) { ... } // each element
break // exit loop early
continue // skip to next iteration
Functions (Ch8)
fun add(a: Int, b: Int): Int {
return a + b
}
fun square(n: Int) = n * n // single-expression
fun greet(name: String = "Player") { } // default argument
Null Safety (Ch2 intro, Ch14)
var target: String? = null // ? = may be null
val len = target?.length // safe call: null if target is null
val safe = target ?: "none" // Elvis: fallback if null
val forced = target!!.length // assert non-null (crashes if null)
if (target != null) { target.length } // smart cast inside the check
Collections (Ch10, Ch12)
val nums = intArrayOf(1, 2, 3) // fixed-size array
val list = mutableListOf<Int>() // growable list
list.add(5); list.removeAt(0); list.size; list[0]
val set = mutableSetOf<String>() // unique values
val map = mutableMapOf<String, Int>() // key -> value
map["arrows"] = 30; map.getValue("arrows"); map.containsKey("x")
Common operations (Ch12 preview, Ch14):
list.filter { it > 0 } // keep matching
list.map { it * 2 } // transform each
list.forEach { ... } // do something with each
list.count { it == 0 }; list.any { it > 0 }
Lambdas (Ch14)
val add = { a: Int, b: Int -> a + b }
val isPositive = { it > 0 } // 'it' = the single parameter
fun repeatAction(times: Int, action: () -> Unit) { ... } // higher-order
Classes (Ch16)
class Spaceship(var x: Float, var y: Float) { // constructor + properties
private var health = 100 // private = hidden
init { /* runs once at creation */ }
fun takeDamage(amount: Int) { health -= amount }
fun isDestroyed(): Boolean = health <= 0
}
val ship = Spaceship(100f, 200f) // instantiate
ship.takeDamage(10)
Inheritance & Polymorphism (Ch18)
open class Enemy(var x: Float) { // open = can be inherited
open fun move() { } // open = can be overridden
}
class Chaser(x: Float) : Enemy(x) {
override fun move() { super.move(); /* ... */ }
}
abstract class Base { // can't be instantiated
abstract fun update() // must be overridden
}
val list = mutableListOf<Enemy>() // holds any Enemy subtype
for (e in list) e.move() // each runs its own version
Interfaces (Ch20)
interface Updatable { fun update() }
interface Drawable { fun draw() }
class Player : Updatable, Drawable { // implement many; extend one class
override fun update() { }
override fun draw() { }
}
Enums (Ch24, used Ch28)
enum class GameState { MENU, PLAYING, GAME_OVER }
when (state) { // exhaustive over enum
GameState.MENU -> ...
GameState.PLAYING -> ...
GameState.GAME_OVER -> ...
}
Files & Packages (Ch19)
- One class per file, named after the class, is the convention.
- Classes in the same package see each other with no import.
importis only needed to reach across packages (e.g.android.graphics.Canvas).