Shallow copy creates a new object but copies references to nested objects instead of the actual objects. Deep copy creates a new object and recursively copies all objects nested within the original, ensuring that there are no shared references between the copy and the original.
When to use:
Example implementation:
// Example class with a nested object class Address { String city; Address(String city) { this.city = city; } } class Person implements Cloneable { String name; Address address; Person(String name, Address address) { this.name = name; this.address = address; } // Shallow copy public Person clone() throws CloneNotSupportedException { return (Person) super.clone(); } // Deep copy public Person deepClone() { return new Person(this.name, new Address(this.address.city)); } }
Can deep copy be implemented using the clone() method?
Answer:
No, the standard Object.clone() method only implements "shallow copy". To obtain a deep copy, all nested fields must be cloned manually or third-party libraries (e.g., Apache Commons Lang) should be used.
Example:
@Override public Person clone() throws CloneNotSupportedException { Person cloned = (Person) super.clone(); cloned.address = new Address(this.address.city); // deep copy manually return cloned; }
Story
In a large CRM project, after implementing object copying via
super.clone(), the developer expected complete independence of copies, but when changing the address of the copied user, the original address also changed. The bug was identified only after the release and caused confusion in the client database.
Story
During serialization/deserialization for deep copying, the standard Serializable mechanism was chosen. However, one of the fields of the nested object was forgotten to be declared as Serializable, leading to errors during runtime (NotSerializableException) and data loss during backups.
Story
In one of the microservices, the BeanUtils.copyProperties library was used for copying DTO, not realizing that the mechanism only implements shallow copy. After refactoring, numerous non-obvious bugs appeared when changes in nested collections affected the original data fetched from the database.