Interview kitsBlog

Your dream job? Lets Git IT.
Interactive technical interview preparation platform designed for modern developers.

XGitHub

Platform

  • Categories

Resources

  • Blog
  • About the app
  • FAQ
  • Feedback

Legal

  • Privacy Policy
  • Terms of Service

© 2026 LetsGit.IT. All rights reserved.

LetsGit.IT/Categories/Kotlin
Kotlineasy

Kotlin null-safety — what do `?`, `?.`, `?:`, and `!!` mean?

Tags
#null-safety#elvis#nullable
Back to categoryPractice quiz

Answer

`T?` is a nullable type, `?.` is a safe call, `?:` (Elvis) provides a default, and `!!` asserts non-null (throws if it’s null).

val name: String? = null
val len = name?.length ?: 0
// val crash = name!!.length // throws if name is null

Advanced answer

Deep dive

Kotlin encodes nullability in the type system.

  • `T?`: value may be null.
  • `?.`: safe call (returns null instead of throwing).
  • `?:`: Elvis (fallback when the left side is null).
  • `!!`: not-null assertion (throws NPE if null) — treat as a last resort.

Practical patterns

  • Prefer safe calls + Elvis:
val len = name?.length ?: 0
  • Use `let` to work only when non-null:
name?.let { println(it.length) }
  • Use `requireNotNull(x)`/`checkNotNull(x)` when null is a programming error.

Common pitfalls

  • Sprinkling `!!` everywhere (you reintroduce NPE).
  • Assuming smart casts always work (they depend on mutability and visibility).

Related questions

Kotlin
`lateinit` vs nullable property: when would you choose `lateinit`?
#kotlin#lateinit#null-safety
Kotlin
Kotlin smart casts: when do they work and when do they not?
#kotlin#smart-cast#type-safety
Kotlin
What is `lateinit` and when can you use it?
#lateinit#null-safety
#properties
Kotlin
Explain null safety in Kotlin.
#null-safety#nullable#elvis-operator