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

Safe Call Operator क्या है

Safe call operator आपको किसी संभावित-खाली box से कुछ मांगने देता है, और अगर box असल में खाली है, तो यह crash होने के बजाय चुपचाप आपको कुछ नहीं वापस दे देता है।
Syntax
kotlin
val result = nullable?.property
val result = nullable?.method()
val result = a?.b?.c          // null if any link is null

Basic Safe Call

?. सिर्फ तभी एक method या property call करता है जब receiver null न हो; अगर receiver null हो, तो पूरा expression बिना throw किए null में short-circuit हो जाता है।

उदाहरण: Basic Safe Call

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 `nullName` (a `String?`), set to `null`
    val nullName: String? = null
    // Print `name?.length` to the console, with a trailing newline
    println(name?.length)
    // Print `nullName?.length` to the console, with a trailing newline
    println(nullName?.length)
}

Chaining Safe Calls

कई ?. operators को साथ chain किया जा सकता है, जैसे user?.address?.city, और पूरी chain जैसे ही इसका कोई हिस्सा null हो null में evaluate हो जाती है।

उदाहरण: Chaining Safe Calls

markup
// Define a data class `Address`
data class Address(val city: String)
// Define a data class `Person`
data class Person(val name: String, val address: Address?)

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `personWithAddress`, set to `Person("Ana", Address("Berlin"))`
    val personWithAddress = Person("Ana", Address("Berlin"))
    // Declare a read-only variable `personWithoutAddress`, set to `Person("Bo", null)`
    val personWithoutAddress = Person("Bo", null)
    // Print `personWithAddress.address?.city` to the console, with a trailing newline
    println(personWithAddress.address?.city)
    // Print `personWithoutAddress.address?.city` to the console, with a trailing newline
    println(personWithoutAddress.address?.city)
}

Safe Calls with Function Calls

एक safe call किसी function को call करने से पहले भी काम करता है, इसलिए text?.trim() सिर्फ तभी trim() चलाता है जब text null न हो, अन्यथा null produce करते हुए।

उदाहरण: Safe Calls with Function Calls

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

Safe Calls in Conditions

Safe calls equality checks के साथ स्वाभाविक रूप से combine होते हैं, जैसे if (text?.length == 0), क्योंकि null की किसी Int से तुलना करना throw करने के बजाय बस false evaluate होता है।

उदाहरण: Safe Calls in Conditions

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `text` (a `String?`), set to `null`
    val text: String? = null
    // Check whether `text?.length == 0`
    if (text?.length == 0) {
        // Print "Empty string" to the console, with a trailing newline
        println("Empty string")
    // Otherwise, run this branch
    } else {
        // Print "Either not empty or null" to the console, with a trailing newline
        println("Either not empty or 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. किसी nullable receiver पर ?. के बजाय . उपयोग करना, जिसे compiler सीधे reject कर देता है।
  2. कई ?. calls को chain करना लेकिन यह भूल जाना कि पूरी chain जैसे ही कोई link null हो null में short-circuit हो जाती है।
  3. यह मान लेना कि ?. null पर एक exception फेंकता है; यह कभी throw नहीं करता, यह बस इसके बजाय null evaluate होता है।
चैप्टर सारांश
  • ?. सिर्फ तभी एक member call करता है जब receiver null न हो; नहीं तो पूरा expression null evaluate होता है।
  • a?.b?.c जैसी chained safe calls जैसे ही chain में कोई link null हो null में short-circuit हो जाती हैं।
  • ?. कभी कोई exception नहीं फेंकता -- यह किसी nullable type पर साधारण . का सुरक्षित विकल्प है।
  • किसी safe call का result हमेशा एक nullable type होता है, क्योंकि यह null हो सकता है।
🔒

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.