Junior — Middle
Is it possible to create two identical objects in Kotlin with the same property values?
sobes.tech AI
Answer from AI
Yes, in Kotlin, you can create two different objects with the same property values. For example, if you use a regular class, then two instances with the same values will be different objects in memory:
class User(val name: String, val age: Int)
val user1 = User("Alice", 30)
val user2 = User("Alice", 30)
println(user1 == user2) // false, because it compares references
However, if you use a data class, Kotlin automatically generates the equals and hashCode methods, and objects with the same values will be considered equal:
data class User(val name: String, val age: Int)
val user1 = User("Alice", 30)
val user2 = User("Alice", 30)
println(user1 == user2) // true
Thus, two objects with the same property values can exist, but their comparison depends on the implementation of equals.