Skip to content
Guest
Kotlin & Android · Course Syllabus Show lessons
Kotlin Core · 8 min read · Intermediate

Kotlin Basics, `val`/`var` & Null Safety

Write clean, expressive Kotlin and eliminate NullPointerExceptions at compile time.

What you'll learn

  • Use `val` by default and `var` only when reassignment is required
  • Handle nullable types safely with `?.` and `?:`
  • Use string templates (`"Hello, $name"`) cleanly

Overview

Write clean, expressive Kotlin and eliminate NullPointerExceptions at compile time.

Code Example

fun greet(name: String?): String {
    val display = name?.trim()?.takeIf { it.isNotEmpty() } ?: "Developer"
    return "Hello, $display"
}

Key Takeaways

  • Use val by default and var only when reassignment is required
  • Handle nullable types safely with ?. and ?:
  • Use string templates ("Hello, $name") cleanly

Quick Check

Check Your Understanding

Score: 0 / 1

1. What does `val display = user?.name ?: "Guest"` return when `user` is `null`?

Study Workspace

Finished this lesson?

Your progress is saved locally in your browser, no account needed.

Search courses, lessons, and references

Type to search across all courses, lessons, and quick references.

Press Esc to close Powered by Pagefind