Programming Languages
Kotlin
Interview
Oct 01, 2025
Explain null safety in Kotlin with examples.
Detailed Explanation
Kotlin eliminates NullPointerException by distinguishing nullable and non-nullable types. Non-nullable: var name: String = "John" (cannot be null), Nullable: var name: String? = null (can be null). Safe call operator: name?.length, Elvis operator: name?.length ?: 0, Not-null assertion: name!!.length (throws exception if null). This prevents runtime crashes from null references.
Discussion (0)
No comments yet. Be the first to share your thoughts!
Share Your Thoughts