From 401d887eee47903610534e0a004f3ba8759347af Mon Sep 17 00:00:00 2001 From: DavidKupper <102686721+DavidKupper@users.noreply.github.com> Date: Thu, 18 Jan 2024 15:38:03 +0100 Subject: [PATCH] Added Null-Safty Chapter to kotlin.md --- source/_posts/kotlin.md | 57 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/source/_posts/kotlin.md b/source/_posts/kotlin.md index 8814341..b4701bd 100644 --- a/source/_posts/kotlin.md +++ b/source/_posts/kotlin.md @@ -437,6 +437,63 @@ myAge != sisterAge // true + +Null-Safety +---- + + + +### Nullable vs Not-Nullable + +```kotlin +var a: String = "Kotlin" // a can never be null +a = null // compilation error +var b: String? = "Kotlin" // b can be null +b = null // ok +``` + + + +### Safe-Calls + +```kotlin +val a = "Kotlin" +val b: String? = null +println(a.length) // can be called safely, because a is never null +println(b?.length) // b?.length returns the length of b, or null if b is null +println(a?.length) // Unnecessary safe call +``` + + + +### Chaining Safe-Calls + +```kotlin +bob?.department?.head?.name // chain returns null if any property is null +``` + + + +### Elvis Operator + +```kotlin +val l = b?.length ?: -1 // if b is null, return the default value -1 +// equval to: +val l: Int = if (b != null) b.length else -1 +``` + + + +### Not Null Assertion Operator + +```kotlin +val l = b!!.length // throws a NullPointerException, if b is null +``` + + + + + Collections ---