Skip to content
Learn Netverks

Lesson

Step 11/36 31% through track

null-safety-basics

Null safety basics

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Null safety basics: the syntax, patterns, and safety habits you need before advancing in Kotlin.

Nullable types are enforced at compile time—fewer NPEs than Java is Kotlin’s headline safety story.

You will apply Null safety basics in contexts like: Android apps, Spring services, and shared KMP modules.

Write Kotlin in main.kt with fun main(), click Run on server—the dev runner kotlinc compiles to a JVM jar and java runs it; use println for output (requires JDK + kotlinc; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Kotlin's type system separates nullable and non-null references—one of the biggest wins over Java Optional patterns and null checks.

Nullable types

var name: String = "Ada"
// name = null  // compile error

var nick: String? = null
println(nick?.length)   // safe call
println(nick ?: "guest") // Elvis default

Operators

  • ?. — safe call returns null if receiver is null
  • ?: — Elvis operator provides default
  • !! — force unwrap (throws NPE—avoid in production)
  • ?.let { } — run block only when non-null

Important interview questions and answers

  1. Q: Why avoid !!?
    A: It throws KotlinNullPointerException—defeats the type system; use safe calls or early returns.
  2. Q: Java interop null?
    A: Platform types from Java are treated as nullable or not at your risk—annotate Java with @Nullable/@NonNull when possible.

Self-check

  1. How do you declare a nullable String?
  2. What does the Elvis operator do?

Pitfall: Avoid !! in production—use safe calls, Elvis, or early returns instead.

Interview prep

Why avoid !!?

Throws NPE and defeats null safety.

Java interop null?

Platform types from Java need careful handling.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • ? vs !!?
  • Safe call chain?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump