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

val और var में अंतर

val एक labeled box बनाता है जिसे आप सिर्फ एक बार भर सकते हैं, जबकि var एक labeled box बनाता है जिसे आप जितनी बार चाहें फिर से भर सकते हैं।
Syntax
kotlin
val name = value        // read-only reference
var name = value        // can be reassigned
name = newValue         // only allowed for var

Declaring with val

val एक reference बनाता है जिसे बिल्कुल एक बार value दी जा सकती है। उसके बाद किसी val को एक नई value assign करने की कोशिश करना एक compile-time error है, जो अनजाने बदलावों को रोकने में मदद करता है।

Note: जहां भी संभव हो val को प्राथमिकता दें; यह code के बारे में सोचना आसान बनाता है।

उदाहरण: Declaring with val

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

Declaring with var

var एक mutable reference बनाता है जिसकी value बाद में = से बदली जा सकती है। इसका उपयोग तब होता है जब किसी value को program के जीवनकाल में genuinely बदलना हो, जैसे एक चलता हुआ counter।

उदाहरण: Declaring with var

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a mutable variable `counter`, initialized to `0`
    var counter = 0
    counter = counter + 1
    counter += 1
    // Print "Counter is now $counter" to the console, with a trailing newline
    println("Counter is now $counter")
}

val Reference vs Mutable Contents

एक val सिर्फ reference को ही lock करता है, ज़रूरी नहीं कि वह जिस चीज़ को point करता है उसे भी। एक mutable list रखने वाला val अब भी आपको items जोड़ने या हटाने देता है -- आप बस बाद में उस val को किसी दूसरी list पर point नहीं कर सकते।

Note: अगर आपको contents को भी unchangeable बनाना है, तो एक immutable collection type उपयोग करें।

उदाहरण: val Reference vs Mutable Contents

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `items`, set to `mutableListOf("apple", "banana")`
    val items = mutableListOf("apple", "banana")
    // Add `"cherry"` to `items`
    items.add("cherry")
    // Print `items` to the console, with a trailing newline
    println(items)
}

Choosing val Over var

Idiomatic Kotlin default रूप से val को प्राथमिकता देता है और सिर्फ तभी var का सहारा लेता है जब किसी variable को reassign होना ज़रूरी हो। यह आदत अनपेक्षित mutation से होने वाले bugs घटाती है और code को समझना आसान बनाती है।

उदाहरण: Choosing val Over var

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `pi`, set to `3.14159`
    val pi = 3.14159
    // Declare a mutable variable `radius`, initialized to `2.0`
    var radius = 2.0
    radius = 3.0
    // Print "Area: ${pi * radius * radius}" to the console, with a trailing newline
    println("Area: ${pi * radius * radius}")
}
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. किसी val को reassign करने की कोशिश करना और एक compiler error पर हैरान होना; val का मतलब read-only है, सिर्फ एक stylistic default नहीं।
  2. दूसरी languages की आदत से var का ज़्यादा उपयोग करना, जबकि Kotlin में ज़्यादातर values को असल में कभी बदलने की ज़रूरत नहीं पड़ती।
  3. यह सोचना कि val object को खुद immutable बना देता है; यह सिर्फ variable को reassign होने से रोकता है, object के अपने contents अब भी बदल सकते हैं।
चैप्टर सारांश
  • val एक read-only reference declare करता है; इसे एक बार assign किया जा सकता है और कभी reassign नहीं।
  • var एक mutable reference declare करता है जिसे जितनी बार ज़रूरत हो reassign किया जा सकता है।
  • default रूप से val को प्राथमिकता देना idiomatic Kotlin है; var का उपयोग सिर्फ तभी करें जब reassignment वाकई ज़रूरी हो।
  • val reference किए object को immutable नहीं बनाता -- एक val list में अब भी items जोड़े जा सकते हैं।
🔒

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.