<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://onnocenter.or.id/wiki/index.php?action=history&amp;feed=atom&amp;title=KOTLIN%3A_Data_Class</id>
	<title>KOTLIN: Data Class - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://onnocenter.or.id/wiki/index.php?action=history&amp;feed=atom&amp;title=KOTLIN%3A_Data_Class"/>
	<link rel="alternate" type="text/html" href="https://onnocenter.or.id/wiki/index.php?title=KOTLIN:_Data_Class&amp;action=history"/>
	<updated>2026-04-10T09:36:36Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.35.4</generator>
	<entry>
		<id>https://onnocenter.or.id/wiki/index.php?title=KOTLIN:_Data_Class&amp;diff=65911&amp;oldid=prev</id>
		<title>Onnowpurbo at 02:05, 24 July 2022</title>
		<link rel="alternate" type="text/html" href="https://onnocenter.or.id/wiki/index.php?title=KOTLIN:_Data_Class&amp;diff=65911&amp;oldid=prev"/>
		<updated>2022-07-24T02:05:47Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;a href=&quot;https://onnocenter.or.id/wiki/index.php?title=KOTLIN:_Data_Class&amp;amp;diff=65911&amp;amp;oldid=65807&quot;&gt;Show changes&lt;/a&gt;</summary>
		<author><name>Onnowpurbo</name></author>
	</entry>
	<entry>
		<id>https://onnocenter.or.id/wiki/index.php?title=KOTLIN:_Data_Class&amp;diff=65807&amp;oldid=prev</id>
		<title>Onnowpurbo: Created page with &quot;Sumber: https://www.tutorialspoint.com/kotlin/kotlin_data_classes.htm   In this chapter, we will learn about Kotlin Data Classes. A Kotlin Data Class is used to hold the data...&quot;</title>
		<link rel="alternate" type="text/html" href="https://onnocenter.or.id/wiki/index.php?title=KOTLIN:_Data_Class&amp;diff=65807&amp;oldid=prev"/>
		<updated>2022-07-18T02:45:19Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;Sumber: https://www.tutorialspoint.com/kotlin/kotlin_data_classes.htm   In this chapter, we will learn about Kotlin Data Classes. A Kotlin Data Class is used to hold the data...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Sumber: https://www.tutorialspoint.com/kotlin/kotlin_data_classes.htm&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In this chapter, we will learn about Kotlin Data Classes. A Kotlin Data Class is used to hold the data only and it does not provide any other functionality apart from holding data.&lt;br /&gt;
&lt;br /&gt;
There are following conditions for a Kotlin class to be defined as a Data Class:&lt;br /&gt;
&lt;br /&gt;
The primary constructor needs to have at least one parameter.&lt;br /&gt;
&lt;br /&gt;
All primary constructor parameters need to be marked as val or var.&lt;br /&gt;
&lt;br /&gt;
Data classes cannot be abstract, open, sealed, or inner.&lt;br /&gt;
&lt;br /&gt;
The class may extend other classes or implement interfaces. If you are using Kotlin version before 1.1, the class can only implement interfaces.&lt;br /&gt;
&lt;br /&gt;
Syntax&lt;br /&gt;
It's simple to define a Kotlin Data Class. If a Kotlin class is marked with data keyword then it becomes a data class. For example:&lt;br /&gt;
&lt;br /&gt;
data class Book(val name: String, val publisher: String, var reviewScore: Int)&lt;br /&gt;
Good thing about Kotlin Data Class is that when you declare a Kotlin Data Class, the compiler generates Constructor, toString(), equals(), hashCode(), and additional copy() and componentN() functions automatically.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
A Kotlin Data Class is instantiated the same way as other Kotlin classes:&lt;br /&gt;
&lt;br /&gt;
data class Book(val name: String, val publisher: String, var reviewScore: Int)&lt;br /&gt;
&lt;br /&gt;
fun main(args: Array&amp;lt;String&amp;gt;) {&lt;br /&gt;
&lt;br /&gt;
   val book = Book(&amp;quot;Kotlin&amp;quot;, &amp;quot;Tutorials Point&amp;quot;, 10)&lt;br /&gt;
   &lt;br /&gt;
   println(&amp;quot;Name = ${book.name}&amp;quot;)&lt;br /&gt;
   println(&amp;quot;Publisher = ${book.publisher}&amp;quot;)&lt;br /&gt;
   println(&amp;quot;Score = ${book.reviewScore}&amp;quot;)&lt;br /&gt;
	  &lt;br /&gt;
}&lt;br /&gt;
When you run the above Kotlin program, it will generate the following output:&lt;br /&gt;
&lt;br /&gt;
Name = Kotlin&lt;br /&gt;
Publisher = Tutorials Point&lt;br /&gt;
Score = 10&lt;br /&gt;
Copy Function&lt;br /&gt;
The copy() function is created automatically when we define a Kotlin Data Class. This copy function can be used to copy an object altering some of its properties but keeping the rest unchanged. Following is an example:&lt;br /&gt;
&lt;br /&gt;
data class Book(val name: String, val publisher: String, var reviewScore: Int)&lt;br /&gt;
&lt;br /&gt;
fun main(args: Array&amp;lt;String&amp;gt;) {&lt;br /&gt;
&lt;br /&gt;
   val original = Book(&amp;quot;Kotlin&amp;quot;, &amp;quot;Tutorials Point&amp;quot;, 10)&lt;br /&gt;
   &lt;br /&gt;
   val copied = original.copy(reviewScore=5)&lt;br /&gt;
   &lt;br /&gt;
   println(&amp;quot;Original Book&amp;quot;)&lt;br /&gt;
   println(&amp;quot;Name = ${original.name}&amp;quot;)&lt;br /&gt;
   println(&amp;quot;Publisher = ${original.publisher}&amp;quot;)&lt;br /&gt;
   println(&amp;quot;Score = ${original.reviewScore}&amp;quot;)&lt;br /&gt;
   &lt;br /&gt;
   println(&amp;quot;Copied Book&amp;quot;)&lt;br /&gt;
   println(&amp;quot;Name = ${copied.name}&amp;quot;)&lt;br /&gt;
   println(&amp;quot;Publisher = ${copied.publisher}&amp;quot;)&lt;br /&gt;
   println(&amp;quot;Score = ${copied.reviewScore}&amp;quot;)&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
When you run the above Kotlin program, it will generate the following output:&lt;br /&gt;
&lt;br /&gt;
Original Book&lt;br /&gt;
Name = Kotlin&lt;br /&gt;
Publisher = Tutorials Point&lt;br /&gt;
Score = 10&lt;br /&gt;
Copied Book&lt;br /&gt;
Name = Kotlin&lt;br /&gt;
Publisher = Tutorials Point&lt;br /&gt;
Score = 5&lt;br /&gt;
toString Function&lt;br /&gt;
The toString() function is also created automatically when we define a Kotlin Data Class. This function returns a string representation of the object. Following is an example:&lt;br /&gt;
&lt;br /&gt;
data class Book(val name: String, val publisher: String, var reviewScore: Int)&lt;br /&gt;
&lt;br /&gt;
fun main(args: Array&amp;lt;String&amp;gt;) {&lt;br /&gt;
&lt;br /&gt;
   val book = Book(&amp;quot;Kotlin&amp;quot;, &amp;quot;Tutorials Point&amp;quot;, 10)&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
   println(book.toString())&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
When you run the above Kotlin program, it will generate the following output:&lt;br /&gt;
&lt;br /&gt;
Book(name=Kotlin, publisher=Tutorials Point, reviewScore=10)&lt;br /&gt;
hashCode() and equals() Functions&lt;br /&gt;
The hasCode()function returns hash code for the object. If two objects are equal, hashCode() returns the same integer value for the objects.&lt;br /&gt;
&lt;br /&gt;
The equals() function returns true if two objects are equal or they have same hasCode value otherwise it returns false.&lt;br /&gt;
&lt;br /&gt;
Following is an example:&lt;br /&gt;
&lt;br /&gt;
data class Book(val name: String, val publisher: String, var reviewScore: Int)&lt;br /&gt;
&lt;br /&gt;
fun main(args: Array&amp;lt;String&amp;gt;) {&lt;br /&gt;
&lt;br /&gt;
   val original = Book(&amp;quot;Kotlin&amp;quot;, &amp;quot;Tutorials Point&amp;quot;, 10)&lt;br /&gt;
   &lt;br /&gt;
   val copy1 = original.copy(reviewScore=5)&lt;br /&gt;
   val copy2 = original.copy(reviewScore=7)&lt;br /&gt;
   &lt;br /&gt;
   println(&amp;quot;Original Hashcode = ${original.hashCode()}&amp;quot;)&lt;br /&gt;
   println(&amp;quot;Copy1 Hashcode = ${copy1.hashCode()}&amp;quot;)&lt;br /&gt;
   println(&amp;quot;Copy2 Hashcode = ${copy2.hashCode()}&amp;quot;)&lt;br /&gt;
   &lt;br /&gt;
   if( copy1.equals(copy2)){&lt;br /&gt;
      println(&amp;quot;Copy1 is equal to Copy2.&amp;quot;)&lt;br /&gt;
   }else{&lt;br /&gt;
      println(&amp;quot;Copy1 is not equal to Copy2.&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
When you run the above Kotlin program, it will generate the following output:&lt;br /&gt;
&lt;br /&gt;
Original Hashcode = 1957710662&lt;br /&gt;
Copy1 Hashcode = 1957710657&lt;br /&gt;
Copy2 Hashcode = 1957710659&lt;br /&gt;
Copy1 is not equal to Copy2.&lt;br /&gt;
Destructuring Declarations&lt;br /&gt;
We can destructure an object into a number of variables using destructing declaration. For example:&lt;br /&gt;
&lt;br /&gt;
data class Book(val name: String, val publisher: String, var reviewScore: Int)&lt;br /&gt;
&lt;br /&gt;
fun main(args: Array&amp;lt;String&amp;gt;) {&lt;br /&gt;
&lt;br /&gt;
   val book = Book(&amp;quot;Kotlin&amp;quot;, &amp;quot;Tutorials Point&amp;quot;, 10)&lt;br /&gt;
   &lt;br /&gt;
   val( name, publisher,reviewScore ) = book&lt;br /&gt;
   &lt;br /&gt;
   println(&amp;quot;Name = $name&amp;quot;)&lt;br /&gt;
   println(&amp;quot;Publisher = $publisher&amp;quot;)&lt;br /&gt;
   println(&amp;quot;Score = $reviewScore&amp;quot;)&lt;br /&gt;
	  &lt;br /&gt;
}&lt;br /&gt;
When you run the above Kotlin program, it will generate the following output:&lt;br /&gt;
&lt;br /&gt;
Name = Kotlin&lt;br /&gt;
Publisher = Tutorials Point&lt;br /&gt;
Score = 10&lt;br /&gt;
Quiz Time (Interview &amp;amp; Exams Preparation)&lt;br /&gt;
Q 1 - What is the purpose of Data Classes in Kotlin :&lt;br /&gt;
&lt;br /&gt;
A - Kotlin Data Classes are defined to hold the data only.&lt;br /&gt;
&lt;br /&gt;
B - Kotlin Data Classes are synonym of abstract classes&lt;br /&gt;
&lt;br /&gt;
C - Kotlin Data Classes are defined to hold data and associated functions&lt;br /&gt;
&lt;br /&gt;
D - All are incorrect about data classes&lt;br /&gt;
&lt;br /&gt;
Q 2 - Which function is not created by default when we define a Kotlin Data Class&lt;br /&gt;
&lt;br /&gt;
A - copy() function&lt;br /&gt;
&lt;br /&gt;
B - toString() function&lt;br /&gt;
&lt;br /&gt;
C - componentN()&lt;br /&gt;
&lt;br /&gt;
D - All the above&lt;br /&gt;
&lt;br /&gt;
Q 2 - What is the function of componentN() in Kotlin Data Class&lt;br /&gt;
&lt;br /&gt;
A - It is used to define new property of the class&lt;br /&gt;
&lt;br /&gt;
B - It is used to count the number of properties in the class&lt;br /&gt;
&lt;br /&gt;
C - It is used to destructure an object into a number of variables&lt;br /&gt;
&lt;br /&gt;
D - All the above&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Referensi==&lt;br /&gt;
&lt;br /&gt;
* https://www.tutorialspoint.com/kotlin/kotlin_data_classes.htm&lt;/div&gt;</summary>
		<author><name>Onnowpurbo</name></author>
	</entry>
</feed>