EMZETT.
Login

Kurz: - Offizielle Seite kotlinlang.org mit “Kotlin Koans” und dem Playground - Android Basics mit Compose (developer.android.com) - Bücher: Kotlin in Action; Übungen: Kotlin Koans, Advent of Code

Teil des Kurses Kotlin

Kapitel 13 von 13 im Kurs Kotlin (Abschnitt „Nachschlagen“). Mit Fortschritt, Quiz und Zertifikat auf der Lernseite.

Grundlagen

val x = 1            // unveränderlich
var y = 2            // veränderlich
const val C = 3      // Konstante (oberste Ebene)
val t: String? = null
fun f(a: Int, b: Int = 0): Int = a + b

Typen

TypBeispielUmwandeln
Int, Long, Short, Byte42, 42LtoInt(), toLong()
Double, Float3.14, 3.14ftoDouble()
Boolean, Char, Stringtrue, 'a', "x"toString(), toBoolean()
Any, Unit, NothingWurzeltyp, “void”, “kehrt nie zurück”
T?nullbar?., ?:, !!, as?

Kontrollfluss

if (a) x else y            when (v) { 1 -> ..; in 2..5 -> ..; is String -> ..; else -> .. }
for (i in 1..10) {}        for (i in 10 downTo 1 step 2) {}      while () {}      do {} while ()
break@label                continue@label                         return@label

Collections

AufgabeFunktion
Umformenmap, flatMap, filter, filterNotNull, zip
Zusammenfassensum, count, fold, reduce, average
Suchenfind, firstOrNull, any, all, none, contains
Sortierensorted, sortedBy, sortedWith, reversed
GruppierengroupBy, partition, associate, groupingBy().eachCount()
Teilentake, drop, chunked, windowed, subList
LazyasSequence(), generateSequence, sequence { yield() }

Klassen

class A(val x: Int, var y: Int = 0) : Basis(), Schnittstelle { init {..} }
open class Basis;  abstract class Form;  interface I { fun f() }
data class P(val a: Int)       sealed class S       enum class E { A, B }
object Singleton               companion object { }
override fun / override val    private set         lateinit var      by lazy { }

Scope-Funktionen

ObjektErgebnis
letitBlockwert
run / withthisBlockwert
applythisObjekt
alsoitObjekt

Versionen: Hinweise

BereichÄlterNeuer (Kotlin 1.4+)
Groß-/KleinschreibungtoUpperCase(), capitalize()uppercase(), replaceFirstChar { it.uppercase() }
SummensumBy { }sumOf { }
Maximamax()/maxBy nullbarmax()/maxBy nicht nullbar (ab 1.7), maxOrNull()
Interfacesnormalfun interface (SAM), sealed interface
ListenmutableListOf().apply { }buildList { }, buildMap { }

Häufige Fehler

Meldung / ProblemUrsache
”Val cannot be reassigned”val statt var verwendet
”Only safe (?.) or non-null asserted (!!.) calls are allowed”nullbarer Typ ohne Prüfung
”Type mismatch: inferred type is Int but Long was expected”keine automatische Zahlenumwandlung
“‘when’ expression must be exhaustive”else oder alle Fälle ergänzen
Klasse lässt sich nicht erbenopen fehlt
NullPointerException trotz Kotlin!! oder Java-Plattformtyp

Weiterlernen

  • Offizielle Seite kotlinlang.org mit “Kotlin Koans” und dem Playground
  • Android Basics mit Compose (developer.android.com)
  • Bücher: Kotlin in Action; Übungen: Kotlin Koans, Advent of Code

Merke

  • val vor var, Typen ableiten lassen, Nullbarkeit im Typ
  • Collections-Funktionen statt Schleifen, when statt Ketten
  • Data/Sealed Classes modellieren Daten und Zustände
  • Coroutinen und Compose sind der moderne Android-Stack

Quiz zur Selbstkontrolle

Weiter im Kurs

Zurück: Coroutinen, Android und Ökosystem

Alle Kapitel: Kotlin im Überblick