What is the difference between deep copy and shallow copy in Java?
In Java, deep copy and shallow copy are two different ways of copying objects.
Shallow copy refers to creating a new object and then copying the field values of the original object into the new object. The new object and the original object share the same child objects. In other words, if the original object has a field of a reference type, then the new object will reference the same child object.
Deep copy refers to creating a new object and then copying the field values of the original object to the new object. The difference is that if the original object has a field of a reference type, then a new sub-object will be created for the new object and its values will be copied.
Specifically, shallow copying only duplicates the values of an object, not the object’s subobjects. In contrast, deep copying not only duplicates an object’s values but also recursively duplicates the object’s subobjects.
Here is an example code illustrating the difference between deep copy and shallow copy:
class Person implements Cloneable {
private String name;
private Address address;
public Person(String name, Address address) {
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Address {
private String city;
public Address(String city) {
this.city = city;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
public class Main {
public static void main(String[] args) throws CloneNotSupportedException {
Address address = new Address("Beijing");
Person person1 = new Person("Alice", address);
// 浅拷贝
Person person2 = (Person) person1.clone();
System.out.println(person1.getAddress().getCity()); // Output: Beijing
System.out.println(person2.getAddress().getCity()); // Output: Beijing
// 修改 person2 的 address
person2.getAddress().setCity("Shanghai");
System.out.println(person1.getAddress().getCity()); // Output: Shanghai
System.out.println(person2.getAddress().getCity()); // Output: Shanghai
// 深拷贝
Address address2 = new Address("Guangzhou");
Person person3 = new Person("Bob", address2);
Person person4 = (Person) person3.clone();
System.out.println(person3.getAddress().getCity()); // Output: Guangzhou
System.out.println(person4.getAddress().getCity()); // Output: Guangzhou
// 修改 person4 的 address
person4.getAddress().setCity("Shenzhen");
System.out.println(person3.getAddress().getCity()); // Output: Guangzhou
System.out.println(person4.getAddress().getCity()); // Output: Shenzhen
}
}
In the example above, person1 and person2 share the same Address object, so modifying person2’s address will also affect person1’s address. This is a characteristic of shallow copy.
Person3 and Person4 have their own Address objects, so modifying Person4’s address will not affect Person3’s address. This is the characteristic of deep copy.