KOTLIN / NOTES</>↗Kotlin28 Jun 2026
In Kotlin, run, apply, also, and with are scope functions. They help make code cleaner by giving you a temporary scope around an object. The main differences are: Function Object reference Returns Best used for apply this The original object Configuring an object also it The original object Side effects, logging, validation run this Lambda […]
KOTLIN / NOTES</>↗Kotlin28 Jun 2026
Use ?.let { … }. The safe-call operator ?. makes sure let is called only when the value is not null. Inside the let block, the value is available as a non-null value, usually named it. val name: String? = "Kotlin" name?.let { println("Name is $it") println("Length is ${it.length}") } If name is "Kotlin", the […]
KOTLIN / NOTES</>↗Kotlin26 Jun 2026
In Kotlin, !! is the not-null assertion operator. It forcefully converts a nullable value like String? into a non-null value like String. val name: String? = getName() val length = name!!.length This tells Kotlin: “Trust me, name is not null.” If name is actually null at runtime, Kotlin throws a NullPointerException. val name: String? = […]
KOTLIN / NOTES</>↗Kotlin26 Jun 2026
Use Kotlin’s Elvis operator ?: with throw on the right-hand side: val value: String? = getNullableValue() val nonNullValue: String = value ?: throw IllegalArgumentException("value must not be null") Because throw is an expression in Kotlin, it can be used after ?:. Example fun printLength(text: String?) { val nonNullText = text ?: throw IllegalArgumentException("text must not […]
KOTLIN / NOTES</>↗Kotlin26 Jun 2026
In Kotlin, the Elvis operator ?: provides a fallback value when the expression on its left is null. val result = nullableValue ?: defaultValue If nullableValue is not null, result gets that value. If nullableValue is null, result gets defaultValue. Example: val name: String? = null val displayName = name ?: "Guest" println(displayName) // Guest […]
KOTLIN / NOTES</>↗Kotlin26 Jun 2026
In Kotlin, the safe call operator ?. lets you access a property or call a function only if the value is not null. If the value is null, the expression simply returns null instead of throwing a NullPointerException. val name: String? = null val length = name?.length println(length) // null Here, name is nullable because […]