Chapter 3: Kotlin Basics

Kotlin Basics! This is where the fun really starts. Up until now, we’ve set up the tools and run a “Hello World” that Android Studio gave us for free. Now we’re going to write real Kotlin code ourselves, understand why it’s so loved, and build simple logic that makes apps smart.

Imagine we’re in a small café in Airoli — I’m your teacher, whiteboard in front, and you’re typing along in Android Studio. We’ll go slow, with tons of examples, analogies, common mistakes (especially for folks coming from Java or other languages), and little exercises at the end.

Quick note on version (Jan 27, 2026): We’re using Kotlin 2.3.0 (released Dec 2025), which is the latest stable. Android Studio Otter 3 ships with great support for it. All examples here work perfectly in your project.

1. Variables & Data Types (The Building Blocks)

In Kotlin, variables are like labeled boxes where you store data.

Two main kinds:

  • val → Immutable (read-only, like final in Java). Use this most of the time — safer!
  • var → Mutable (can change later).

Kotlin has type inference — you often don’t need to write the type; it guesses from the value.

Basic types (similar to Java but nicer):

Kotlin

String interpolation magic (one of Kotlin’s best features):

Kotlin

Common mistake: Forgetting to use val first → always prefer val unless you really need to change it.

2. Operators (Math & Comparisons)

Same as most languages, but clean:

Kotlin

Logical operators:

Kotlin

3. Control Flow (Decisions & Loops)

if / else — works as expression (can return value!):

Kotlin

when — Kotlin’s super-powered switch (best feature!):

Kotlin

Loops:

  • for (most common for ranges/collections):
Kotlin
  • while / do-while:
Kotlin

4. Functions (Reusable Code Blocks)

Functions in Kotlin are concise.

Basic:

Kotlin

With return:

Kotlin

Default & named arguments:

Kotlin

5. Null Safety (Kotlin’s Superpower — No More NPEs!)

Kotlin prevents null pointer exceptions at compile time.

  • Types are non-null by default:
Kotlin
  • Nullable with ?:
Kotlin

6. Collections (Lists, Maps, Sets)

Kotlin has powerful, immutable-first collections.

Kotlin

Hands-on: Simple Kotlin Exercises (Do These Now!)

Open Android Studio → Your HelloWebliance project → Create a new Kotlin file (right-click kotlin folder → New → Kotlin Class/File) → Name it KotlinPlayground.kt

Paste and run these one by one (add fun main() around each exercise):

  1. Basic variables & string:
    Kotlin
  2. When expression:
    Kotlin
  3. Function with default param:
    Kotlin
  4. Null safety challenge:
    Kotlin
  5. Collection fun:
    Kotlin

Run them by right-clicking the file → Run ‘KotlinPlaygroundKt.main()’

Play around! Change values, break things, see errors — that’s how you learn.

Whew — that’s Kotlin basics covered in depth! You now have the tools to think logically in code.

Questions? Stuck on an exercise? Want to add one of these to your Compose app (like show name in Text)? Tell me — next chapter we’ll make your app interactive with state & buttons. You’re killing it! Keep typing, keep experimenting. 🚀💻

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *