← Back to Kotlin Course | Chapter 2: Variables & Types | Lesson 7 of 7

Type Checking और Casting

Type checking और casting आपके code को यह पूछने देते हैं 'यह किस तरह की चीज़ है?' और फिर इसे उस specific kind के रूप में सुरक्षित रूप से treat करने देते हैं।
Syntax
kotlin
if (value is Type) {
    // value is smart-cast to Type here
}

val casted = value as Type       // ClassCastException if wrong
val safe = value as? Type       // null if wrong

Checking Types with is

is operator runtime पर यह जांचता है कि कोई value किसी दिए गए type का instance है या नहीं, true या false return करते हुए, Java में instanceof जैसा।

उदाहरण: Checking Types with is

markup
// Define the function `describe` taking `value`
fun describe(value: Any) {
    // Check whether `value is String`
    if (value is String) {
        // Print "It is a String of length ${value.length}" to the console, with a trailing newline
        println("It is a String of length ${value.length}")
    // Otherwise, run this branch
    } else {
        // Print "Not a String" to the console, with a trailing newline
        println("Not a String")
    }
}

// Entry point: execution of the program starts here
fun main() {
    describe("Kotlin")
    describe(42)
}

Smart Casts

एक बार जब कोई is check किसी value का type पुष्टि कर दे, Kotlin अपने आप उस scope के बाकी हिस्से के लिए इसे उस type के रूप में treat करता है -- किसी manual cast की ज़रूरत नहीं, जिसे smart cast कहा जाता है।

उदाहरण: Smart Casts

markup
fun printLength(value: Any) {
    if (value is String) {
        // value is smart-cast to String here
        println("Length: ${value.length}")
    }
}

fun main() {
    printLength("Hello")
}

Unsafe Cast with as

as operator किसी value को ज़बरदस्ती किसी दिए गए type में cast करता है, अगर value असल में उस type के साथ compatible न हो तो runtime पर एक ClassCastException फेंकते हुए।

Note: as का उपयोग सिर्फ तभी करें जब आपको type के बारे में निश्चितता हो; अन्यथा as? को प्राथमिकता दें।

उदाहरण: Unsafe Cast with as

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `value` (a `Any`), set to `"Kotlin"`
    val value: Any = "Kotlin"
    // Declare a read-only variable `text`, set to `value as String`
    val text = value as String
    // Print "Cast succeeded: $text" to the console, with a trailing newline
    println("Cast succeeded: $text")
}

Safe Cast with as?

as? operator वही cast करने की कोशिश करता है लेकिन fail होने पर throw करने के बजाय null return करता है, जो type के अनिश्चित होने पर कहीं ज़्यादा सुरक्षित है।

उदाहरण: Safe Cast with as?

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `value` (a `Any`), set to `42`
    val value: Any = 42
    // Declare a read-only variable `text`, set to `value as? String`
    val text = value as? String
    // Print "Safe cast result: $text" to the console, with a trailing newline
    println("Safe cast result: $text")
}
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. किसी value पर unsafe cast as उपयोग करना जो असल में वह type नहीं हो सकती, runtime पर एक ClassCastException का कारण बनते हुए।
  2. किसी is check के बाद smart casting उपयोग न करना, और फिर भी manually cast करना, ज़रूरत से ज़्यादा verbose code पैदा करते हुए।
  3. यह भूल जाना कि as? throw करने के बजाय null return करता है, और उस null result को handle न करना।
चैप्टर सारांश
  • is runtime पर यह जांचता है कि कोई value एक खास type है या नहीं, एक Boolean return करते हुए।
  • Kotlin के smart casts किसी if (x is Type) block के अंदर बिना किसी manual cast के किसी value को अपने आप checked type के रूप में treat करते हैं।
  • as एक unsafe cast करता है जो value असल में वह type न होने पर ClassCastException फेंकता है।
  • as? एक safe cast करता है जो cast संभव न होने पर throw करने के बजाय null return करता है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 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.