Câu hỏi phỏng vấn Kotlin
Câu hỏi

What is a data class in Kotlin?

Câu trả lời

A data class in Kotlin is a special type of class primarily used to hold data. It is marked with the data keyword and provides several automatically generated functions to reduce boilerplate code. These functions include:

  • equals(): Checks if two objects are equal based on their property values.
  • hashCode(): Generates a hash code for the object.
  • toString(): Returns a string representation of the object.
  • copy(): Creates a copy of the object with the ability to modify some properties.
  • componentN(): Functions corresponding to the properties in their order of declaration, useful for destructuring declarations.

Requirements for Data Classes

To ensure consistency and meaningful behavior, data classes must fulfill the following requirements:

  1. The primary constructor must have at least one parameter.
  2. All primary constructor parameters must be marked as val (read-only) or var (read-write).
  3. Data classes cannot be abstract, open, sealed, or inner...
junior

junior

Gợi ý câu hỏi phỏng vấn

middle

What is the difference between const and val ?

middle

What is a purpose of Companion Objects in Kotlin?

middle

val mutableList vs var immutableList . When to use which in Kotlin?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào