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

Nullable Types क्या हैं

एक nullable type किसी box को पूरी तरह खाली रहने की अनुमति देने का Kotlin का तरीका है, ताकि compiler आपको लापरवाही से अंदर झांकने से पहले चेतावनी दे सके।
Syntax
kotlin
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

markup
// 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")
}

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

markup
// 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)
}

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

markup
// 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)
}

Function Parameters as Nullable

किसी function parameter को nullable चिह्नित करना callers को स्पष्ट रूप से बताता है कि null पास करना एक उम्मीद के मुताबिक, valid case है जिसे function खुद handle करेगा।

उदाहरण: Function Parameters as Nullable

markup
// 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))
}
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. किसी type के बाद ? भूल जाना जब कोई value genuinely अनुपस्थित हो सकती है, जिससे compiler इसे null assign करने से मना कर देता है।
  2. किसी nullable type को एक regular type जैसा treat करना और सीधे एक method call करना, जिसे compiler तब तक reject करता है जब तक null case handle न हो।
  3. सावधानी से हर जगह 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:

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.