Kotlin

Kotlin Data Class

A data class in Kotlin is defined as a class that is created for the sole purpose of holding data in it. However, there are certain built-in functions that are not needed to be defined by the user but they can easily be used with the data class such as copy, toString, hashCode, equals, component, etc. In this article, we will introduce you to the usage of the Kotlin data class while paying a special emphasis on these built-in functions.

Using the Data Class in Kotlin in Ubuntu 20.04

For using the data class in Kotlin in Ubuntu 20.04, you can take help from the following five examples:

Example # 1: Creating a Basic Data Class in Kotlin

In this example, we want to teach you the method of creating a basic data class in Kotlin and accessing its members. The sample script for this example is as follows:

In this sample script, we have first declared the data class by using the “data” and “class” keywords. The name of our data class is “Student” and it has two data members i.e., “name” of type “String” and “age” of type “Integer”. Then, within our driver function, we have created an instance of this class “s1” and have assigned to it a random name and age. After that, we have simply printed the values of the data members of our data class on the terminal.

We then compiled this script with the command given below:

$ kotlinc dataclass.kt

After that, we executed the class file with the following command:

$ kotlin DataclassKt

The values of the data members of our data class are shown in the image below:

Example # 2: Using the Copy Function of the Data Class in Kotlin

In this example, we will teach you the method of using the built-in “copy” function of the Kotlin data class. For that, we have formulated the following Kotlin script:

In this script, we have used the same data class that we have created in the example above. Then, we have created the first instance of this class named “s1”. After that, we have created another instance of this class named “s2” and have copied “s1” to “s2” while changing the name of the student. Then, we have simply printed the names and ages of both the students on the terminal.

The names and ages of both of our students are shown in the image below:

Example # 3: Using the Hash Code and Equal Functions of the Data Class in Kotlin

In this example, we will teach you the process of using the “hash code” and “equal” functions of the data class in Kotlin. The hash code function returns the hash value associated with a particular instance of the data class. The equal function compares the hash values of two instances and returns true if the two instances are equal, otherwise, false. The sample script for this example is as follows:

Again, we have used the very same data class in this example. Then, we have created the first instance of this class by passing a name and age to this instance. After that, we have created the second instance of this class by copying the first instance to it while changing the name. On the other hand, we have created a third instance of this class simply by copying the first instance to it entirely without changing anything. Then, we have just printed the hash values of all these instances on the terminal. After that, we have compared the first instance with the second by using the “equals” function. In the same way, we have compared the first instance with the third. The result of this function will decide the output of our program.

The output of this Kotlin program is shown in the image below. It shows that our first and second instances are different from each other whereas the first and third instances are exactly the same.

Example # 4: Using the String Function of the Data Class in Kotlin

In this example, we will share with you the usage of the string function of the Kotlin data class. The string function of the Kotlin data class is simply used to print the contents of an instance of a data class in the form of a string. The sample script for this purpose is as follows:

After defining the student data class in this example, we have instantiated an object of this class. Then, we have simply used the “toString()” function with the name of this instance to return its contents in the form of a string.

The output of this program in the form of a string is shown in the image below:

Example # 5: Using the Component Function of the Data Class in Kotlin

Finally, in this example, we will tell you how you can use the component function of the Kotlin data class. This function is to access the different components of an instance of a data class. The example script for this purpose is shown in the following image:

We have used the same data class in this example that we have created in the example above. However, this time we have accessed both the components of the instance of this class by using the “component1” and “component2” notations.

The components of the instance of our data class are shown in the image below:

Conclusion

In this article, we wanted you to learn about the concept of the data class in Kotlin. We first taught you why this class is used in Kotlin in the first place. After that, we explained to you five different examples in which we have made use of all the built-in functions of the data class in Kotlin in Ubuntu 20.04. By going through this article, you can easily learn the usage of the Kotlin data class as well as all the functions associated with it.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.