Junior — Middle
Can you explain what is meant by shallow copying of an object?
sobes.tech AI
Answer from AI
Shallow copying of an object is creating a new object in which all the fields of the original object are copied, but if the fields contain references to other objects, only these references are copied, not the nested objects themselves. That is, only the top level is copied, and nested objects remain shared between the original and the new object.
For example, if you have an object with a list field, then in shallow copying, the new object will contain a reference to the same list, not a copy of it. Changes to this list will be visible in both the original and the new object.
In Kotlin, for shallow copying of a data class, you can use the copy() method, which copies only the first-level fields.