Kotlin Essentials

The Kotlin language for Android: null safety, data classes, lambdas, collections and coroutines basics.

Beginner · about 10 hours · difficulty 2/5

If you chose native Android, Kotlin is the official language. It is concise, null-safe and interoperable with Java libraries.

What to learn

  • val vs var: prefer immutable val.
  • Null safety: String vs String?, safe calls ?., the Elvis operator ?:, and why !! is a smell.
  • Functions: default and named arguments, single-expression functions, extension functions.
  • Classes: data class for models, sealed class/sealed interface for UI states, object for singletons.
  • Lambdas and collections: map, filter, groupBy, trailing lambda syntax (Compose relies on it heavily).
  • when expressions for exhaustive branching over sealed types.

Coroutines preview

Coroutines let you write asynchronous code sequentially using suspend functions. On Android you launch them in a lifecycle-aware scope like viewModelScope. You will go deeper in the Android section.

Pitfalls

Java habits (getters/setters, nullable everything, mutable lists) make Kotlin code verbose and fragile. Lean on the idioms.

Good enough to move on: you can model a screen's state with a sealed interface and handle every case with when.

Key concepts

  • val, var and type inference
  • Null safety operators
  • Data classes and sealed types
  • Lambdas and trailing lambda syntax
  • Collection operations
  • Extension functions
  • Coroutines and suspend functions

Mini project

Write a Kotlin console program that models a shopping cart with data classes and prints a receipt grouped by category.

Check yourself

Why is a sealed interface a better way to represent Loading, Success and Error states than three nullable fields?

Free resources