← Back to Kotlin Course | Chapter 5: Null Safety | Lesson 4 of 6

Non-Null Assertion क्या है

Non-null assertion आप Kotlin को यह बता रहे हैं कि 'मुझ पर भरोसा करो, यह निश्चित रूप से खाली नहीं है', भले ही compiler पूरी तरह निश्चित न हो सके।
Syntax
kotlin
val value = nullable!!    // throws NullPointerException if null

val safer = nullable ?: defaultValue
val safer = nullable?.member

Using the !! Operator

किसी nullable expression में !! जोड़ना compiler को assert करता है कि value निश्चित रूप से null नहीं है, direct, non-nullable-style access की अनुमति देते हुए -- लेकिन अगर वह assumption गलत हो तो यह runtime पर तुरंत throw करता है।

उदाहरण: Using the !! Operator

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `name` (a `String?`), set to `"Kotlin"`
    val name: String? = "Kotlin"
    // Declare a read-only variable `length`, set to `name!!.length`
    val length = name!!.length
    // Print "Length: $length" to the console, with a trailing newline
    println("Length: $length")
}

The Danger of !! on Actual Nulls

अगर asserted value runtime पर असल में null हो, !! तुरंत एक NullPointerException फेंकता है, जो बिल्कुल वही तरह का crash है जिसे रोकने के लिए Kotlin की null safety डिज़ाइन की गई थी।

उदाहरण: The Danger of !! on Actual Nulls

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `name` (a `String?`), set to `"Kotlin"`
    val name: String? = "Kotlin"
    try {
        // Declare a read-only variable `other` (a `String?`), set to `null`
        val other: String? = null
        // Print `other!!.length` to the console, with a trailing newline
        println(other!!.length)
    } catch (e: NullPointerException) {
        // Print "Caught NPE: as expected when forcing a null value" to the console, with a trailing newline
        println("Caught NPE: as expected when forcing a null value")
    }
    // Print "Safe name length: ${name!!.length}" to the console, with a trailing newline
    println("Safe name length: ${name!!.length}")
}

Preferring Safer Alternatives

लगभग हर case में, ?., ?:, या एक explicit if (value != null) check intent को ज़्यादा स्पष्ट रूप से बताता है और crashing से बचाता है, !! को अनावश्यक बनाते हुए।

Note: !! का सहारा सिर्फ तभी लें जब आप आस-पास के logic से यह साबित कर सकें कि value बिल्कुल null नहीं हो सकती।

उदाहरण: Preferring Safer Alternatives

markup
fun main() {
    val name: String? = null
    // Prefer this over `name!!.length`
    val length = name?.length ?: 0
    println("Safe length: $length")
}

When !! Can Be Justified

एक दुर्लभ स्वीकार्य उपयोग एक null check के ठीक बाद है जो ऐसे तरीके से किया गया हो जिसे compiler smart-cast नहीं कर सकता, जैसे अलग से store किए किसी Boolean flag के ज़रिए, जहां developer ने genuinely पहले ही null को खारिज कर दिया हो।

उदाहरण: When !! Can Be Justified

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `values`, set to `mutableMapOf("count" to 5)`
    val values = mutableMapOf("count" to 5)
    // Check whether `values.containsKey("count")`
    if (values.containsKey("count")) {
        // Print "Count is ${values["count"]!!}" to the console, with a trailing newline
        println("Count is ${values["count"]!!}")
    }
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. compiler warnings को चुप कराने के एक तेज़ तरीके के रूप में !! का ज़्यादा उपयोग करना, उन्हीं null pointer exceptions को वापस लाते हुए जिन्हें Kotlin का type system रोकने की कोशिश करता है।
  2. किसी user input या network response से आई value पर !! उपयोग करना, जहां nullness runtime पर genuinely अनिश्चित है।
  3. यह भूल जाना कि अगर value असल में null हो तो !! तुरंत एक NullPointerException (technically KotlinNullPointerException) फेंकता है।
चैप्टर सारांश
  • !! किसी nullable value को ज़बरदस्ती non-null treat करता है, अगर यह असल में null हो तो तुरंत एक exception फेंकते हुए।
  • !! को आखिरी उपाय होना चाहिए, सिर्फ तभी उपयोग करें जब आपको निश्चितता हो कि उस बिंदु पर value null नहीं हो सकती।
  • !! का ज़्यादा उपयोग Kotlin के null-safety system के मकसद को हरा देता है और null pointer crashes वापस ला सकता है।
  • ?., ?:, और explicit if checks जैसे सुरक्षित विकल्प लगभग हमेशा !! से बेहतर होते हैं।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.