There is a specific class in Kotlin, whose purpose is to hold the data of the object, that class is known as Kotlin data class. In this tutorial, you will learn all about the Kotlin data class with the help of some examples.

Here's How to Land a Top Software Developer Job

Full Stack Developer - MERN StackExplore Program
Here's How to Land a Top Software Developer Job

What Is a Data class in Kotlin?

The data class in Kotlin is used to hold the data. This data class contains some utility functions that are often derivable from the data. 

With the help of the data class, you don’t need to write the boiler plate code. The compiler automatically generates all the getter and setter for all the data class properties. Its syntax includes the data keyword with class.

Kotlin_Data_Class_1

Now, go ahead and understand the requirement for the Kotlin data class.

Requirements of Data Class

You need to take care of some requirements while creating a data class.

  • The primary constructor must have at least one parameter.
  • The parameter of the primary constructor must be val or var.
  • The data class cannot be abstract, sealed, or open.
  • The data class can extend other classes and can implement the interface.

Now, have a look at the Functions of Kotlin data class.

Functions of Data Class

The data class includes some inbuilt functions, and due to these functions, you don’t need to write the boiler plate code. Following are those functions:

  • toString()

  • copy()

  • equals()
  • hashCode()

toString()

The data class has the toString() function, which can display the object's data just by printing the reference of the class.

The toString() function converts the object to a string and prints the string representation of the object. The syntax of the output will be in a particular format.

Kotlin_Data_Class_2

Now, go ahead and have a look at its example.

Kotlin_Data_Class_3.

Here you have used the data class, and you know the data class contains the toString() function, which will convert the object to a string. So when you print the object p, the output would be as follows.

Kotlin_Data_Class_4

Now, you will understand the copy() function.

Learn the Ins & Outs of Software Development

Caltech Coding BootcampExplore Program
Learn the Ins & Outs of Software Development

copy()

In data class, you can easily create a copy of an object with the help of the copy() function. This copy() function helps in altering the properties of the object.

Kotlin_Data_Class_5

As you can see, object r2 is copying the object r1, which means the properties of object r1 are getting copied to object r2. The object r3 is copying the properties of r1 and also altering or modifying the properties of r1. You can notice the roll number is getting altered.

Below is the output of the above example.

Kotlin_Data_Class_6

equals() and hashCode()

There are two more functions in the data class: equals() and hashCode(). The equal() function returns true or false; if two objects are equal, it will return true, else, it will return false. 

Here, look at an example where equal() function is being used.

Kotlin_Data_Class_7

Below is the output of the above example.

Kotlin_Data_Class_8

The hashCode() function returns the hashcode of the object, i.e. if the objects are equal, then the hashcode will print the same integer output.

Kotlin_Data_Class_9

Below is the output of the above example.

Kotlin_Data_Class_10

Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

Conclusion

In this tutorial on Kotlin data class, you understood what Kotlin Data class is, and learned the requirements to create the data class. You also learned about the Functions of data classes like toString(), copy(), hashCode(), etc, and understood them in detail with the help of examples. 

If you are looking to build a software development career, you can check the Post-Graduate Program in Full Stack Development by Simplilearn. It can be the ideal solution to help you build your career in the right direction.

Do you have any questions regarding this tutorial on Kotlin Data class? If you do, then you can put them in the comments section. We’ll help you solve your queries. 

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 16 Dec, 2024

6 Months$ 8,000
Automation Test Engineer Masters Program

Cohort Starts: 27 Nov, 2024

8 months$ 1,499
Full Stack Java Developer Masters Program

Cohort Starts: 18 Dec, 2024

7 months$ 1,449
Full Stack (MERN Stack) Developer Masters Program

Cohort Starts: 8 Jan, 2025

6 Months$ 1,449