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

Type Inference क्या है

Type inference का मतलब है कि Kotlin बिना आपके बताए यह पता लगाने के लिए काफी smart है कि किसी variable में किस तरह की value है।
Syntax
kotlin
val name = value              // type inferred
val name: Type = value        // explicit type
var name = value              // type stays fixed

How Inference Works

जब आप val age = 25 लिखते हैं, Kotlin value 25 को देखता है और infer करता है कि age का type Int है, बिना आपके कहीं Int लिखे। यह compile time पर होता है, इसलिए type fixed है और उतनी ही सख्ती से जांचा जाता है जितना अगर आपने इसे लिखा होता।

उदाहरण: How Inference Works

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `age`, set to `25`
    val age = 25
    // Declare a read-only variable `price`, set to `19.99`
    val price = 19.99
    // Declare a read-only variable `name`, set to `"Kotlin"`
    val name = "Kotlin"
    // Print "$name costs $price for someone aged $age" to the console, with a trailing newline
    println("$name costs $price for someone aged $age")
}

Explicit Type Annotations

आप हमेशा colon के बाद explicitly type लिख सकते हैं, जैसे val age: Int = 25। यह तब ज़रूरी है जब infer करने के लिए कोई initial value न हो, और कुछ cases में readability भी सुधार सकता है।

उदाहरण: Explicit Type Annotations

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `age` (an Int), set to `25`
    val age: Int = 25
    // Declare a read-only variable `price` (a Double), set to `19.99`
    val price: Double = 19.99
    // Print "Age: $age, Price: $price" to the console, with a trailing newline
    println("Age: $age, Price: $price")
}

Inference and Static Typing

भले ही type हमेशा न लिखा जाए, Kotlin statically typed बना रहता है: एक बार infer होने के बाद, किसी variable का type बदल नहीं सकता, और mismatched type assign करना एक compile-time error है।

Note: इसके बाद score = "high" करने की कोशिश compile नहीं होगी -- inferred type Int lock हो चुका है।

उदाहरण: Inference and Static Typing

markup
fun main() {
    var score = 100 // inferred as Int
    score = 105     // fine, still an Int
    println("Score: $score")
}

When Inference Is Not Possible

बिना किसी initial value के declared एक val या var के पास infer करने का कोई आधार नहीं, इसलिए उस स्थिति में एक explicit type चाहिए, और variable को उपयोग होने से पहले assign किया जाना चाहिए।

उदाहरण: When Inference Is Not Possible

markup
// Entry point: execution of the program starts here
fun main() {
    val result: Int
    result = 10 * 4
    // Print "Result: $result" to the console, with a trailing newline
    println("Result: $result")
}
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. यह मान लेना कि Kotlin dynamically typed है क्योंकि आप शायद ही कभी explicitly types लिखते हैं; Kotlin statically typed है, यह सिर्फ compile time पर types infer करता है।
  2. किसी val को uninitialized छोड़ना और फिर भी Kotlin से type infer करने की उम्मीद करना; inference को काम करने के लिए एक initial value चाहिए।
  3. आदत से हर जगह redundant explicit types जोड़ना, code को ज़रूरत से ज़्यादा verbose बनाते हुए।
चैप्टर सारांश
  • Kotlin किसी variable का type उसकी initial value से compile time पर infer करता है, runtime पर नहीं।
  • Kotlin statically typed है -- हर variable का एक fixed type होता है, भले ही वह type explicitly न लिखा गया हो।
  • Type inference को एक initializer चाहिए; बिना किसी value वाला अकेला val x infer नहीं हो सकता।
  • Explicit type annotations अब भी स्पष्टता के लिए या जब infer करने के लिए कोई initializer न हो तब उपयोगी हैं।
🔒

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.