kotlinbeginner
Null Safety — Elvis, Safe Call, and let
Master Kotlin null safety: safe calls, Elvis operator, let/also scoping, and smart casts.
kotlinPress ⌘/Ctrl + Shift + C to copy
fun main() {
// Nullable type
val name: String? = "Alice"
val missing: String? = null
// Safe call operator ?.
println(name?.length) // 5
println(missing?.length) // null (no NPE)
// Elvis operator ?:
val len = missing?.length ?: 0
println("Length: $len") // 0
// Elvis with throw
fun getUser(id: Int): String =
findUser(id) ?: throw IllegalArgumentException("User $id not found")
// let — execute block if non-null
name?.let { n ->
println("Name is $n with ${n.length} chars")
}
// also — side effects
val upper = name?.also { println("Processing: $it") }
?.uppercase()
println(upper) // ALICE
// Safe casts
val value: Any = "hello"
val str: String? = value as? String // "hello"
val num: Int? = value as? Int // null (safe)
println("str=$str, num=$num")
// Chained safe calls
data class Address(val city: String?)
data class User(val address: Address?)
val user: User? = User(Address("NYC"))
val city = user?.address?.city ?: "Unknown"
println("City: $city") // NYC
// requireNotNull / checkNotNull
val config: String? = System.getenv("APP_CONFIG")
// val required = requireNotNull(config) { "APP_CONFIG must be set" }
// Smart cast after null check
fun process(input: String?) {
if (input != null) {
// Smart cast: input is String here
println(input.uppercase())
}
// Or with require
input ?: return
println(input.lowercase()) // Smart cast
}
process("test")
}
fun findUser(id: Int): String? = if (id == 1) "Alice" else nullUse Cases
- Safe navigation through nullable chains
- Default values for missing data
- Null-safe API consumption
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
kotlinbeginner
Kotlin Null Safety Complete Guide
Master null safety: safe calls, elvis operator, let/also/run, smart casts, and nullable collection handling.
Best for: Safe handling of nullable data from APIs
#kotlin#null-safety
kotlinbeginner
Data Classes — Copy, Destructure, and Equals
Use data classes for immutable models: auto-generated equals, hashCode, copy, and destructuring.
Best for: Immutable domain models and DTOs
#kotlin#data-class
kotlinbeginner
Scope Functions — let, run, apply, also, with
Master Kotlin scope functions: when to use let, run, apply, also, and with for concise code.
Best for: Object initialization and configuration
#kotlin#scope-functions
kotlinbeginner
Collections — map, filter, groupBy, and More
Master Kotlin collections: functional transformations, aggregations, grouping, and partition operations.
Best for: Data processing and transformation pipelines
#kotlin#collections