Nullable Types क्या हैं
In this page:
var name: Type? = value
name = null
if (name != null) {
// name is smart-cast to Type
}
Nullable vs Non-Nullable Types
किसी type के अंत में ? जोड़ना, जैसे String?, इसे nullable चिह्नित करता है, यानी उस type का कोई variable या तो एक असली value या null रख सकता है। एक साधारण String (बिना ?) कभी null नहीं हो सकता।
उदाहरण: Nullable vs Non-Nullable Types
// Entry point: execution of the program starts here
fun main() {
// Declare a mutable variable `name` (a String), initialized to `"Kotlin"`
var name: String = "Kotlin"
// Declare a mutable variable `nickname` (a `String?`), initialized to `null`
var nickname: String? = null
// Print "Name: $name, Nickname: $nickname" to the console, with a trailing newline
println("Name: $name, Nickname: $nickname")
}
Login to try C/C++/Java/PHP code in the editor
The Compiler Prevents Null Pointer Exceptions
चूंकि type system nullability को track करता है, compiler ऐसे code को compile करने से मना कर देता है जो किसी null value पर कोई method call करके crash हो सकता है, आपको उस संभावना को explicitly handle करने के लिए मजबूर करते हुए।
उदाहरण: The Compiler Prevents Null Pointer Exceptions
// Define the function `printLength` taking `text`
fun printLength(text: String?) {
// Check whether `text != null`
if (text != null) {
// Print "Length: ${text.length}" to the console, with a trailing newline
println("Length: ${text.length}")
// Otherwise, run this branch
} else {
// Print "Text is null" to the console, with a trailing newline
println("Text is null")
}
}
// Entry point: execution of the program starts here
fun main() {
printLength("Kotlin")
printLength(null)
}
Login to try C/C++/Java/PHP code in the editor
Smart Casting a Nullable Value
if (text != null) जैसे null check के बाद, Kotlin उस block के अंदर text को उसके non-nullable type में smart-cast कर देता है, इसलिए इसे किसी अतिरिक्त unwrapping के बिना बिल्कुल एक normal String की तरह उपयोग किया जा सकता है।
उदाहरण: Smart Casting a Nullable Value
// Define the function `shout` taking `text`
fun shout(text: String?) {
// Check whether `text != null`
if (text != null) {
// Print `text.toUpperCase()` to the console, with a trailing newline
println(text.toUpperCase())
}
}
// Entry point: execution of the program starts here
fun main() {
shout("hello")
shout(null)
}
Login to try C/C++/Java/PHP code in the editor
Function Parameters as Nullable
किसी function parameter को nullable चिह्नित करना callers को स्पष्ट रूप से बताता है कि null पास करना एक उम्मीद के मुताबिक, valid case है जिसे function खुद handle करेगा।
उदाहरण: Function Parameters as Nullable
// Define the function `describeAge` taking `age` and returning a String
fun describeAge(age: Int?): String {
// Return `if (age == null) "Age unknown" else "Age is $age"` from this function
return if (age == null) "Age unknown" else "Age is $age"
}
// Entry point: execution of the program starts here
fun main() {
// Print `describeAge(30)` to the console, with a trailing newline
println(describeAge(30))
// Print `describeAge(null)` to the console, with a trailing newline
println(describeAge(null))
}
Login to try C/C++/Java/PHP code in the editor
- किसी type के बाद
?भूल जाना जब कोई value genuinely अनुपस्थित हो सकती है, जिससे compiler इसेnullassign करने से मना कर देता है। - किसी nullable type को एक regular type जैसा treat करना और सीधे एक method call करना, जिसे compiler तब तक reject करता है जब तक null case handle न हो।
- सावधानी से हर जगह nullable types का ज़्यादा उपयोग करना, जबकि कई values को शुरू से ही कभी null न होने के लिए design किया जा सकता था।
?के बाद एक type, जैसेString?, या तो उस type की एक value याnullरख सकता है।- एक non-nullable type, जैसे साधारण
String, कभीnullनहीं रख सकता; compiler इसे compile time पर enforce करता है। - null case handle किए बिना किसी nullable type पर सीधे कोई member call करना एक compile-time error है।
- Kotlin की null safety program चलने से पहले ही कई null pointer exceptions पकड़ लेती है।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: