← Back to Kotlin Course | Chapter 6: Classes & Objects | Lesson 5 of 7

Data Classes क्या हैं

एक data class सिर्फ जानकारी रखने वाली classes के लिए एक खास shortcut है, ताकि Kotlin अपने आप आपके लिए boring comparison और printing code लिख दे।
Syntax
kotlin
data class ClassName(val property1: Type, val property2: Type)

val a = ClassName(value1, value2)
val b = a.copy(property2 = newValue)
val (x, y) = a

Declaring a Data Class

class से पहले data जोड़ना compiler से useful boilerplate -- equals(), hashCode(), और toString() -- generate करने के लिए कहता है, primary constructor में properties के आधार पर।

उदाहरण: Declaring a Data Class

markup
// Define a data class `Point`
data class Point(val x: Int, val y: Int)

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `p`, set to `Point(3, 4)`
    val p = Point(3, 4)
    // Print `p` to the console, with a trailing newline
    println(p)
}

Automatic equals() and hashCode()

एक जैसी property values वाले दो data class instances को == द्वारा बराबर माना जाता है, क्योंकि equals() सभी constructor properties को compare करने के लिए generate होता है।

उदाहरण: Automatic equals() and hashCode()

markup
// Define a data class `Point`
data class Point(val x: Int, val y: Int)

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `p1`, set to `Point(1, 2)`
    val p1 = Point(1, 2)
    // Declare a read-only variable `p2`, set to `Point(1, 2)`
    val p2 = Point(1, 2)
    // Print "Equal: ${p1 == p2}" to the console, with a trailing newline
    println("Equal: ${p1 == p2}")
}

Copying with Modifications

generated copy() function एक जैसी property values वाला एक नया instance बनाता है, आपको सिर्फ उन्हें override करने देते हुए जिन्हें आप बदलना चाहते हैं।

उदाहरण: Copying with Modifications

markup
// Define a data class `User`
data class User(val name: String, val age: Int)

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `original`, set to `User("Ana", 30)`
    val original = User("Ana", 30)
    // Declare a read-only variable `older`, set to `original.copy(age = 31)`
    val older = original.copy(age = 31)
    // Print "$original -> $older" to the console, with a trailing newline
    println("$original -> $older")
}

Destructuring a Data Class

Data classes अपने आप destructuring declarations support करती हैं, आपको उनकी properties को उस order में अलग variables में unpack करने देते हुए जिस order में वे declared थीं।

उदाहरण: Destructuring a Data Class

markup
// Define a data class `Point`
data class Point(val x: Int, val y: Int)

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `point`, set to `Point(5, 10)`
    val point = Point(5, 10)
    val (x, y) = point
    // Print "x=$x, y=$y" to the console, with a trailing newline
    println("x=$x, y=$y")
}
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. एक normal class लिखना और manually equals, hashCode, और toString implement करना जब data class तीनों मुफ़्त में generate कर देता।
  2. यह भूल जाना कि copy() एक shallow copy करता है, इसलिए एक ही nested object को reference करने वाली mutable properties अब भी original और copy के बीच share होती हैं।
  3. properties को primary constructor के बाहर रखना और यह उम्मीद करना कि वे generated equals/toString में शामिल होंगी; सिर्फ constructor properties शामिल होती हैं।
चैप्टर सारांश
  • data class अपने आप equals(), hashCode(), toString(), और एक copy() function generate करता है primary constructor properties के आधार पर।
  • copy() एक जैसी values वाला एक नया instance बनाता है, optionally specific properties override करते हुए।
  • Data classes componentN() functions के ज़रिए कई variables में destructuring भी support करती हैं।
  • सिर्फ primary constructor में declared properties generated functions में हिस्सा लेती हैं।
🔒

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.