- Kotlin - Exception Handling
- Kotlin - Destructuring Declarations
- Kotlin - Delegation
- Kotlin - Generics
- Kotlin - Sealed Class
- Kotlin - Data Classes
- Kotlin - Extension
- Kotlin - Visibility Control
- Kotlin - Interface
- Kotlin - Abstract Classes
- Kotlin - Inheritance
- Kotlin - Constructors
- Kotlin - Class and Objects
- Kotlin - Maps
- Kotlin - Sets
- Kotlin - Lists
- Kotlin - Collections
- Kotlin - Break and Continue
- Kotlin - While Loop
- Kotlin - For Loop
- Kotlin - When Expression
- Kotlin - if...Else Expression
- Kotlin - Control Flow
- Kotlin - Functions
- Kotlin - Ranges
- Kotlin - Arrays
- Kotlin - Strings
- Kotlin - Booleans
- Kotlin - Operators
- Kotlin - Data Types
- Kotlin - Variables
- Kotlin - Keywords
- Kotlin - Comments
- Kotlin - Basic Syntax
- Kotlin - Architecture
- Kotlin - Environment Setup
- Kotlin - Overview
- Kotlin - Home
Kotlin Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Kotpn - Data Classes
In this chapter, we will learn about Kotpn Data Classes. A Kotpn Data Class is used to hold the data only and it does not provide any other functionapty apart from holding data.
There are following conditions for a Kotpn class to be defined as a Data Class:
The primary constructor needs to have at least one parameter.
All primary constructor parameters need to be marked as val or var.
Data classes cannot be abstract, open, sealed, or inner.
The class may extend other classes or implement interfaces. If you are using Kotpn version before 1.1, the class can only implement interfaces.
Syntax
It s simple to define a Kotpn Data Class. If a Kotpn class is marked with data keyword then it becomes a data class. For example:
data class Book(val name: String, val pubpsher: String, var reviewScore: Int)
Good thing about Kotpn Data Class is that when you declare a Kotpn Data Class, the compiler generates Constructor, toString(), equals(), hashCode(), and additional copy() and componentN() functions automatically.
Example
A Kotpn Data Class is instantiated the same way as other Kotpn classes:
data class Book(val name: String, val pubpsher: String, var reviewScore: Int) fun main(args: Array<String>) { val book = Book("Kotpn", "Tutorials Point", 10) println("Name = ${book.name}") println("Pubpsher = ${book.pubpsher}") println("Score = ${book.reviewScore}") }
When you run the above Kotpn program, it will generate the following output:
Name = Kotpn Pubpsher = Tutorials Point Score = 10
Copy Function
The copy() function is created automatically when we define a Kotpn 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:
data class Book(val name: String, val pubpsher: String, var reviewScore: Int) fun main(args: Array<String>) { val original = Book("Kotpn", "Tutorials Point", 10) val copied = original.copy(reviewScore=5) println("Original Book") println("Name = ${original.name}") println("Pubpsher = ${original.pubpsher}") println("Score = ${original.reviewScore}") println("Copied Book") println("Name = ${copied.name}") println("Pubpsher = ${copied.pubpsher}") println("Score = ${copied.reviewScore}") }
When you run the above Kotpn program, it will generate the following output:
Original Book Name = Kotpn Pubpsher = Tutorials Point Score = 10 Copied Book Name = Kotpn Pubpsher = Tutorials Point Score = 5
toString Function
The toString() function is also created automatically when we define a Kotpn Data Class. This function returns a string representation of the object. Following is an example:
data class Book(val name: String, val pubpsher: String, var reviewScore: Int) fun main(args: Array<String>) { val book = Book("Kotpn", "Tutorials Point", 10) println(book.toString()) }
When you run the above Kotpn program, it will generate the following output:
Book(name=Kotpn, pubpsher=Tutorials Point, reviewScore=10)
hashCode() and equals() Functions
The hasCode()function returns hash code for the object. If two objects are equal, hashCode() returns the same integer value for the objects.
The equals() function returns true if two objects are equal or they have same hasCode value otherwise it returns false.
Following is an example:
data class Book(val name: String, val pubpsher: String, var reviewScore: Int) fun main(args: Array<String>) { val original = Book("Kotpn", "Tutorials Point", 10) val copy1 = original.copy(reviewScore=5) val copy2 = original.copy(reviewScore=7) println("Original Hashcode = ${original.hashCode()}") println("Copy1 Hashcode = ${copy1.hashCode()}") println("Copy2 Hashcode = ${copy2.hashCode()}") if( copy1.equals(copy2)){ println("Copy1 is equal to Copy2.") }else{ println("Copy1 is not equal to Copy2.") } }
When you run the above Kotpn program, it will generate the following output:
Original Hashcode = 1957710662 Copy1 Hashcode = 1957710657 Copy2 Hashcode = 1957710659 Copy1 is not equal to Copy2.
Destructuring Declarations
We can destructure an object into a number of variables using destructing declaration. For example:
data class Book(val name: String, val pubpsher: String, var reviewScore: Int) fun main(args: Array<String>) { val book = Book("Kotpn", "Tutorials Point", 10) val( name, pubpsher,reviewScore ) = book println("Name = $name") println("Pubpsher = $pubpsher") println("Score = $reviewScore") }
When you run the above Kotpn program, it will generate the following output:
Name = Kotpn Pubpsher = Tutorials Point Score = 10
Quiz Time (Interview & Exams Preparation)
Q 1 - What is the purpose of Data Classes in Kotpn :
Answer : A
Explanation
Kotpn Data Classes are defined to hold the data only.
Q 2 - Which function is not created by default when we define a Kotpn Data Class
Answer : D
Explanation
All the mentioned functions are created automatically when we define Kotpn Data Class.
Q 2 - What is the function of componentN() in Kotpn Data Class
Answer : C
Explanation
The function componentN() is used to destructure an object into a number of variables.
Advertisements