Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Data Classes | Object-Oriented Programming in Kotlin
Android Development with Kotlin
course content

Conteúdo do Curso

Android Development with Kotlin

Android Development with Kotlin

1. Kotlin Basics
2. Control Flow and Collections
3. Object-Oriented Programming in Kotlin

Data Classes

In Kotlin, there are not only regular classes but also a structure known as data classes.

Data classes are a special type of class designed for storing data. They simplify the creation of classes that primarily contain properties and don't have complex logic.

Data classes differ from regular classes in their usage and application.

Let’s look at an illustration that highlights the main differences:

Key Features

Let’s talk a bit about the key features of data classes to better understand their use.

Automatically Generated Methods

  • equals(): Compares two objects for equality based on the values of their properties;
  • hashCode(): Generates a hash code for the object based on its properties;
  • toString(): Creates a string representation of the object in the format - ClassName(property1=value1, property2=value2, ...);
  • copy(): Creates a copy of the object with the option to modify some properties.

Declaration Syntax

The declaration of a data class begins with the data keyword, followed by the class name and constructor parameters.

For example:

kt

Person

copy
1
data class Person(val name: String, val age: Int)

Destructuring

Data classes support destructuring, which allows you to extract the values of properties into variables.

Example:

kt

Example

copy
123
val person = Person("Alice", 25) val (name, age) = person println("Name: $name, Age: $age") // Output: Name: Alice, Age: 25

Let's take a closer look at destructuring.

Destructuring in Kotlin is the process of unpacking an object into individual variables. It allows you to quickly extract values from an object, especially from data classes.

For example, let’s consider another example of destructuring:

kt

Example

copy
1234567
data class Person(val name: String, val age: Int, val city: String) fun main() { val person = Person("John", 30, "New York") val (name, age, city) = person println("Name: $name, Age: $age, City: $city") }

Here, the person object is broken down into the variables name, age, and city, which are then used separately.

It can be concluded that data classes are very convenient for storing data, which is why they are called data classes.

Let's look at a couple of examples to reinforce and better understand the material:

kt

Main

copy
12345678910111213141516
data class Book(val title: String, val author: String, val year: Int) fun main() { val book1 = Book("1984", "George Orwell", 1949) val book2 = Book("1984", "George Orwell", 1949) println(book1) // Output: Book(title=1984, author=George Orwell, year=1949) println(book1 == book2) // Output: true val book3 = book1.copy(year = 1950) println(book3) // Output: Book(title=1984, author=George Orwell, year=1950) val (title, author, year) = book1 println("Title: $title, Author: $author, Year: $year") // Output: Title: 1984, Author: George Orwell, Year: 1949 }

In the code above, the following actions are performed:

  • Objects book1, book2, and book3 are created, with book3 being copied with a change in the year;
  • The comparison between book1 and book2 shows that they are equal in content;
  • The destructuring of book1 breaks it down into the variables title, author, and year, which are then printed separately.

Let's now imagine that we have a data class to describe a user and another class to describe an address.

We can use them together:

kt

Main

copy
12345678910
data class Address(val street: String, val city: String, val zipCode: String) data class User(val name: String, val age: Int, val address: Address) fun main() { val address = Address("221B Baker Street", "London", "NW1 6XE") val user = User("Sherlock Holmes", 40, address) val updatedUser = user.copy(address = address.copy(city = "New York")) println(updatedUser) // Output: User(name=Sherlock Holmes, age=40, address=Address(street=221B Baker Street, city=New York, zipCode=NW1 6XE)) }

This way, we can manipulate data and create classes for storing specific information.

Data classes are an excellent choice when you need to create a class primarily for storing data and don't require complex logic.

What is a key feature of a data class in Kotlin?

Selecione a resposta correta

Tudo estava claro?

Seção 3. Capítulo 3
We're sorry to hear that something went wrong. What happened?
some-alt