66. The purpose of the “!!” operator in Kotlin
In Kotlin, the !! operator is called the "not-null assertion operator".
It asserts that a nullable variable is not null — and throws a NullPointerException (NPE) if it is.
val name: String? = null
val length = name!!.length // 🔥 Throws NullPointerException
It tells the compiler: “I know this is not null. Trust me.” 😬
The !! operator bypasses Kotlin’s null-safety — one of the key features of the language.
Using it:
67. The purpose of the “?.” operator in Kotlin
The ?. operator in Kotlin is called the safe call operator.
It is used to safely access a property or call a method on a nullable object—without throwing a NullPointerException.
Syntax
val result = nullableObject?.someMethodOrProperty
If nullableObject is:
Example
val name: String? = null
val length = name?.length
println(length) // Output: null (no crash)
68. The purpose of the “?:” operator in Kotlin
The ?: operator in Kotlin is called the Elvis operator.
Syntax:
val result = nullableValue ?: defaultValue
If nullableValue is not null, it's returned; otherwise, defaultValue is used.
val name: String? = null
val displayName = name ?: "Guest"
println(displayName) // Output: Guest
If name were "John", it would output John.
Common Use Cases
Use Case | Example |
---|---|
Default value for nullable variables | val city = user.city ?: "Unknown" |
Return fallback from functions | val age = getUserAge() ?: -1 |
Avoid null pointer exceptions | val length = text?.length ?: 0 |
Without Elvis:
val result: Int
if (x != null) {
result = x
} else {
result = 0
}
With Elvis:
val result = x ?: 0
Use with throw or return
val user = getUser() ?: throw IllegalStateException("User not found")
69. The purpose of the “let” operator in Kotlin
In Kotlin, the let operator is a scope function used to execute a block of code on a non-null object or to perform operations within a scoped context.
Syntax
object?.let { it ->
// use 'it' here
}
Or with named parameter:
object?.let { value ->
println(value)
}
Run code only if the object is not null:
val name: String? = "Bind the Logic"
name?.let {
println("Length: ${it.length}")
}
If name is null, block won't run.
You can perform transformations without assigning intermediate variables:
val result = "kotlin".let {
it.uppercase()
}.reversed()
println(result) // => NILTOK
3. Chaining or Function Pipelines
val final = " hello "
.trim()
.let { it.uppercase() }
.let { "Greeting: $it" }
println(final) // Greeting: HELLO
val file = File("demo.txt")
file.let {
println("File path: ${it.absolutePath}")
}
let is best when you want to:
Use Case | Why Use let |
---|---|
Null-safe call | Executes block only when not null |
Scoped transformation | Cleaner chaining & logic |
Temporary variable usage | Keeps scope tight and readable |
Chaining functions | Useful in pipelines |
70. The concept of value classes in Kotlin
Value classes in Kotlin are a feature designed to create lightweight wrappers around a single value without the performance overhead of traditional objects. They’re particularly useful when you want type safety but don’t need the full power of a regular class.
Basic Syntax
@JvmInline
value class Email(val value: String)
This creates a type-safe Email wrapper around a String.