

Its primary focus is on increasing productivity, readability, and safety for developers. You can use the copy() function to copy an existing object into a new object and modify some of the properties while keeping the existing object unchanged. Kotlin is a statically-typed programming language developed by JetBrains. Kotlin makes working with immutable data objects easier by automatically generating a copy() function for all the data classes. Since they can not be modified after creation, you don’t need to worry about concurrency issues that arise when multiple threads try to modify an object at the same time. This means if you follow the chain of parents of a class you will finally end up at Any.

Immutable objects are easier to work with and reason about while working with multi-threaded applications. This means Any is the parent class of any class you define in Kotlin. Let’s understand what these functions do and how to use them - Data Classes and Immutability: The copy() functionĪlthough the properties of a data class can be mutable (declared using var), It’s strongly recommended to use immutable properties (declared using val) so as to keep the instances of the data class immutable. Public class Customer " ) // Prints -1841845792Īpart from the standard methods like equals(), hashCode() and toString(), Kotlin also generates a copy() function and componentN() functions for all the data classes.
