WEB Button DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Kotlin - Data Classes

In this chapter we discuss Data classes of Kotlin programming language.

we can make Data class whenever it is marked as “data"

Whenever a class is marked as data, we can use some of the inbuilt function of that data class such as "toString()","hashCode()", etc.

fun main(args: Array) { val book: Book = Book("Kotlin", "welookups.com", 5) println("Name of the Book is--"+book.name) // "Kotlin" println("Puclisher Name--"+book.publisher) // "welookups.com" println("Review of the book is--"+book.reviewScore) // 5 book.reviewScore = 7 println("Printing all the info all together--"+book.toString()) //using inbuilt function of the data class println("Example of the hashCode function--"+book.hashCode()) } data class Book(val name: String, val publisher: String, var reviewScore: Int)

The above piece of code will yield the following output in the browser, where we have created one data class to hold some of the data, and from the main function we have accessed all of its data members.

ame of the Book is--"Kotlin"
Puclisher Name--"welookups.com"
Review of the book is--5
Printing all the info all together--(name-Kotlin, publisher-welookups.com, reviewScore-7)
Example of the hashCode function---1753517245ame of the Book is--"Kotlin"
Puclisher Name--"TutorialPoint.com"
Review of the book is--5
Printing all the info all together--(name-Kotlin, publisher-welookups.com, reviewScore-7)
Example of the hashCode function---1753517245