<p>This tutorial is about Kotlin Data classes, requirements that data class must fulfill, and their standard functionalities.</p>
<h4><span style="color: #000080;"><strong>Data Classes</strong></span></h4>
<p class="p1">Data classes are classes whose main purpose is to hold data/state and contains standard functionalities. A <span style="color: #0000ff;"><strong>data</strong></span> keyword is used to declare a class as a data class.</p>
<p><strong><span style="color: #0000ff;">Example:</span></strong></p>
<pre>data class Student(val name: String, val age: Int)</pre>
<p class="p1">Data class internally contains the following functions:</p>
<ul class="ul1">
<li class="li1"><strong><span style="color: #0000ff;">equals(): Boolean</span></strong></li>
<li class="li1"><strong><span style="color: #0000ff;">hashCode(): Int</span></strong></li>
<li class="li1"><strong><span style="color: #0000ff;">toString(): String</span></strong></li>
<li class="li1"><strong><span style="color: #0000ff;">component() functions corresponding to the properties</span></strong></li>
<li class="li1"><strong><span style="color: #0000ff;">copy()</span></strong></li>
</ul>
<p class="p1">Due to presence of above functions internally in data class, the data class eliminates the boilerplate code.</p>
<h5><span style="color: #000080;"><strong>Data Class Example</strong></span></h5>
<pre><span style="color: #008000;"><strong>data</strong></span> class Student(val name: String, val age: Int)

fun main() {
 val s1 = Student("Rocky", 29)
 println("name = ${s1.name}")
 println("age = ${s1.age}")
}</pre>
<p><span style="color: #0000ff;"><strong>Output:</strong></span></p>
<pre>name = Rocky
age = 29</pre>
<p class="p1">When you declare a <span style="color: #0000ff;"><strong>data</strong></span> class, the compiler automatically generates several functions such as <span style="color: #008000;"><strong>toString()</strong></span>, <strong><span style="color: #008000;">equals()</span></strong>, <span style="color: #008000;"><strong>hashcode()</strong></span> etc behind the scenes.</p>
<h4 class="p1"><span style="color: #000080;"><b>Data Class Requirements</b></span></h4>
<ul class="ul1">
<li class="li1">The <strong><span style="color: #008000;">primary</span> </strong>constructor must have at least one parameter.</li>
<li class="li1">The parameters of the primary constructor must be marked as either <strong><span style="color: #008000;">val</span></strong> (read-only) or <strong><span style="color: #008000;">var</span></strong> (read-write).</li>
<li class="li1">The class cannot be <strong><span style="color: #008000;">open</span></strong>, <strong><span style="color: #008000;">abstract</span></strong>, <strong><span style="color: #008000;">inner</span></strong> or <strong><span style="color: #008000;">sealed</span></strong>.</li>
<li class="li1">The class may extend other classes or implement interfaces. If you are using Kotlin version before <span style="color: #0000ff;"><strong>1.1</strong></span>, the class can only implement interfaces.</li>
</ul>
<h4 id="properties-declared-in-the-class-body" data-toc="data-classes#properties-declared-in-the-class-body"><span class="article__header" style="color: #000080;"><strong><span class="article__title">Properties declared in the class body</span></strong></span></h4>
<p class="p1">The compiler only uses the properties defined inside the primary constructor for the automatically generated functions. To exclude a property from the generated implementations, declare it inside the class body:</p>
<pre>data class Student(val name: String) {
 var age: Int = 0
}</pre>
<p>Only the property <strong><span style="color: #0000ff;">name</span></strong> will be used inside the <span style="color: #008000;"><strong>toString(), equals(), hashCode(), </strong><span style="color: #000000;">and</span><strong> copy()</strong></span> implementations, and there will only be one component function <strong><span style="color: #008000;">component1()</span></strong>.</p>
<p class="p1">Let&#8217;s understand these functions:</p>
<h4><span style="color: #000080;"><strong>toString() method</strong></span></h4>
<p class="p1">Let&#8217;s see a simple program without data class.</p>
<p><strong><span style="color: #0000ff;">Example:</span></strong></p>
<pre>class Student(val name: String, val age: Int)

fun main() {
 val s1 = Student("Rocky", 29)
 println(s1)
}</pre>
<p class="p1">Here, we are printing the reference of <span style="color: #008000;"><b>Student</b></span> object which displays the <span style="color: #008000;"><strong>hashCode()</strong> </span>with class name of <span style="color: #008000;"><b>Student</b></span>. It does not print the data.</p>
<p><strong><span style="color: #0000ff;">Output:</span></strong></p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>

<pre>Student@49476842</pre>
<p class="p1">The above program is rewritten using <span style="color: #0000ff;"><b>data class</b></span> and printing the reference of <span style="color: #008000;"><b>Student</b></span> class and displaying the data of object. It happens because the data class internally contains the <span style="color: #000080;"><strong>toString()</strong></span> <strong>which display the string representation of object</strong>.</p>
<p><strong><span style="color: #0000ff;">Example:</span></strong></p>
<pre>data class Student(val name: String, val age: Int)

fun main() {
 val s1 = Student("Rocky", 29)
 println(s1)
<strong><span style="color: #008000;">//or</span></strong>
 println(s1.toString())
}</pre>
<p><strong><span style="color: #0000ff;">Output:</span></strong></p>
<pre>Student(name=Rocky, age=29)
Student(name=Rocky, age=29)</pre>
<h4 class="h3"><span style="color: #000080;"><strong>copy() method</strong></span></h4>
<p class="p1">The data class provides a <span style="color: #008000;"><strong>copy()</strong></span> method which is used to create a copy of object. Using copy() method, some or all properties of object can be altered.</p>
<p><span style="color: #0000ff;"><strong>Example:</strong></span></p>
<pre>data class Student(val name: String, val age: Int)

fun main() {
 val s1 = Student("Wilson", 29)

 <span style="color: #008000;"><strong>// using copy function to create an object</strong></span>
 val s2 = s1.copy(name = "Rocky")

 println(s1)
 println(s2)
}</pre>
<p><span style="color: #0000ff;"><strong>Output:</strong></span></p>
<pre>Student(name=Wilson, age=29)
Student(name=Rocky, age=29)</pre>
<h4><span style="color: #000080;"><strong>hashCode() and equals()</strong></span></h4>
<p><strong><span style="color: #0000ff;">hashCode():</span></strong> returns a <span style="color: #008000;"><strong>hash code</strong></span> <strong><span style="color: #008000;">value</span></strong> for the object.</p>
<ul>
<li>Two hash codes declared two times on same object will be equal.</li>
<li>If two objects are equal according to <span style="color: #008000;"><strong>equals</strong>()</span> method, then the hash codes<br />
returned will also be same.</li>
</ul>
<p><strong><span style="color: #0000ff;">equals():</span></strong> return <strong><span style="color: #008000;">true</span></strong> if two objects have same contents and it works similar to <strong><span style="color: #0000ff;">“==”</span></strong>.</p>
<p><strong><span style="color: #0000ff;">Example:</span></strong></p>
<pre>data class Student(val name: String, val age: Int)

fun main() {
 val s1 = Student("Rocky", 29)
 val s2 = s1.copy()
 val s3 = s1.copy(name = "Wilson")

 println("s1 : $s1 hashcode : ${s1.hashCode()}")
 println("s2 : $s2 hashcode : ${s2.hashCode()}")
 println("s3 : $s3 hashcode : ${s3.hashCode()}")

 if (s1.equals(s2) == true)
 println("s1 is equal to s2.")
 else
 println("s1 is not equal to s2.")

 if (s1.equals(s3) == true)
 println("s1 is equal to s3.")
 else
 println("s1 is not equal to s3.")
}</pre>
<p><strong><span style="color: #0000ff;">Output:</span></strong></p>
<pre class="p1">s1 : Student(name=Rocky, age=29) hashcode : -1841810167
s2 : Student(name=Rocky, age=29) hashcode : -1841810167
s3 : Student(name=Wilson, age=29) hashcode : -1282221179
s1 is equal to s2.
s1 is not equal to s3.</pre>
<h4 id="data-classes-and-destructuring-declarations"><span style="color: #000080;"><strong>Destructuring Declarations</strong></span></h4>
<p class="p1">You can destructure an object into a number of variables using <span style="color: #0000ff;"><strong><a style="color: #0000ff;" href="https://kotlinlang.org/docs/reference/multi-declarations.html"><span class="s1">destructing declaration</span></a></strong></span>.</p>
<p><strong><span style="color: #0000ff;">Example:</span></strong></p>
<pre>data class Student(val name: String, val age: Int, val gender: String)

fun main() {
 val s1 = Student("Rocky", 29, "Male")

 val (name, age, gender) = s1
 println("name = $name")
 println("age = $age")
 println("gender = $gender")
}</pre>
<p><strong><span style="color: #0000ff;">Output:</span></strong></p>
<pre>name = Rocky
age = 29
gender = Male</pre>
<p>This is possible because the compiler generates componentN() functions for all properties of a data class (defined inside the primary constructor).</p>
<p><strong><span style="color: #0000ff;">Example:</span></strong></p>
<pre>data class Student(val name: String, val age: Int, val gender: String)

fun main() {
 val s1 = Student("Rocky", 29, "Male")

 println(s1.component1()) // Rocky
 println(s1.component2()) // 29 
 println(s1.component3()) //Male
}</pre>
<p><strong><span style="color: #0000ff;">Output:</span></strong></p>
<pre>Rocky
29
Male</pre>
<p> ;</p>


