Non-Null Assertion क्या है
In this page:
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
// 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")
}
Login to try C/C++/Java/PHP code in the editor
The Danger of !! on Actual Nulls
अगर asserted value runtime पर असल में null हो, !! तुरंत एक NullPointerException फेंकता है, जो बिल्कुल वही तरह का crash है जिसे रोकने के लिए Kotlin की null safety डिज़ाइन की गई थी।
उदाहरण: The Danger of !! on Actual Nulls
// 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}")
}
Login to try C/C++/Java/PHP code in the editor
Preferring Safer Alternatives
लगभग हर case में, ?., ?:, या एक explicit if (value != null) check intent को ज़्यादा स्पष्ट रूप से बताता है और crashing से बचाता है, !! को अनावश्यक बनाते हुए।
!! का सहारा सिर्फ तभी लें जब आप आस-पास के logic से यह साबित कर सकें कि value बिल्कुल null नहीं हो सकती।उदाहरण: Preferring Safer Alternatives
fun main() {
val name: String? = null
// Prefer this over `name!!.length`
val length = name?.length ?: 0
println("Safe length: $length")
}
Login to try C/C++/Java/PHP code in the editor
When !! Can Be Justified
एक दुर्लभ स्वीकार्य उपयोग एक null check के ठीक बाद है जो ऐसे तरीके से किया गया हो जिसे compiler smart-cast नहीं कर सकता, जैसे अलग से store किए किसी Boolean flag के ज़रिए, जहां developer ने genuinely पहले ही null को खारिज कर दिया हो।
उदाहरण: When !! Can Be Justified
// 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"]!!}")
}
}
Login to try C/C++/Java/PHP code in the editor
- compiler warnings को चुप कराने के एक तेज़ तरीके के रूप में
!!का ज़्यादा उपयोग करना, उन्हीं null pointer exceptions को वापस लाते हुए जिन्हें Kotlin का type system रोकने की कोशिश करता है। - किसी user input या network response से आई value पर
!!उपयोग करना, जहां nullness runtime पर genuinely अनिश्चित है। - यह भूल जाना कि अगर value असल में
nullहो तो!!तुरंत एकNullPointerException(technicallyKotlinNullPointerException) फेंकता है।
!!किसी nullable value को ज़बरदस्ती non-null treat करता है, अगर यह असल मेंnullहो तो तुरंत एक exception फेंकते हुए।!!को आखिरी उपाय होना चाहिए, सिर्फ तभी उपयोग करें जब आपको निश्चितता हो कि उस बिंदु पर valuenullनहीं हो सकती।!!का ज़्यादा उपयोग Kotlin के null-safety system के मकसद को हरा देता है और null pointer crashes वापस ला सकता है।?.,?:, और explicitifchecks जैसे सुरक्षित विकल्प लगभग हमेशा!!से बेहतर होते हैं।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: