In Java, Deep Copy (or Deep Cloning) refers to the process of creating a completely independent copy of an object, including all the objects referenced within it. This ensures that any changes made to the cloned object do not affect the original object and vice versa. In deep copy:
- Primitive fields: Are copied by value, just like in a shallow copy.
- Non-primitive fields (objects, arrays, collections): Are copied by creating new objects rather than referencing the same ones.
Deep-CopyExample 1: In this example, we will perform a Deep Copy using the clone() method.
Java
class Address implements Cloneable {
String city;
Address(String city){
this.city = city;
}
@Override
protected Object clone()
throws CloneNotSupportedException{
// Clone the Address object
return super.clone();
}
}
class Person implements Cloneable{
String name;
Address addr;
Person(String name, Address addr){
this.name = name;
this.addr = addr;
}
@Override
protected Object clone()
throws CloneNotSupportedException{
// Create a shallow copy first
Person cloned = (Person)super.clone();
// Now clone the nested Address object manually
cloned.addr = (Address)addr.clone();
// Return the deep-copied object
return cloned;
}
}
public class DeepCopyExample {
public static void main(String[] args)
throws CloneNotSupportedException{
Address a1 = new Address("London");
Person p1 = new Person("Alice", a1);
// Create deep copy
Person p2 = (Person)p1.clone();
// Display original and copied city before change
System.out.println("Original City: "
+ p1.addr.city);
System.out.println("Copied City: " + p2.addr.city);
// Modify copied object's address
p2.addr.city = "Paris";
// Observe changes
System.out.println("\nAfter modification:");
System.out.println("Original City: "
+ p1.addr.city);
System.out.println("Copied City: " + p2.addr.city);
}
}
OutputOriginal City: London
Copied City: London
After modification:
Original City: London
Copied City: Paris
Explanation:
- The Address object is cloned separately within the Person class’s clone() method.
- As a result, both p1 and p2 have independent copies of the Address object.
- Changing p2.addr.city does not affect p1.addr.city, confirming that a deep copy was created.
Example 2: Deep Copy using Copy Constructor
Java
class Address{
String city;
Address(String city){
this.city = city;
}
// Copy constructor for deep copy
Address(Address other){
this.city = other.city;
}
}
class Person{
String name;
Address addr;
Person(String name, Address addr){
this.name = name;
this.addr = addr;
}
// Copy constructor for deep copy
Person(Person other){
this.name = other.name;
this.addr = new Address(
other.addr); // deep copy of Address
}
}
public class GFG{
public static void main(String[] args){
Address a1 = new Address("New York");
Person p1 = new Person("Bob", a1);
// Deep copy using copy constructor
Person p2 = new Person(p1);
System.out.println("Original City: "
+ p1.addr.city);
System.out.println("Copied City: " + p2.addr.city);
// Modify copied object’s address
p2.addr.city = "Los Angeles";
System.out.println("\nAfter modification:");
System.out.println("Original City: "
+ p1.addr.city);
System.out.println("Copied City: " + p2.addr.city);
}
}
OutputOriginal City: New York
Copied City: New York
After modification:
Original City: New York
Copied City: Los Angeles
Explanation: The copy constructor creates a new Address object instead of copying the reference, ensuring that the nested object is also cloned. Hence, changes made in p2 don’t affect p1.
Advantages of Deep Copy
- Independent Objects: Changes in the copied object do not affect the original.
- Safe for Nested Objects: Clones referenced objects to avoid shared references.
- Prevents Side Effects: Modifying one object does not impact others.
- Maintains Data Integrity: Original data remains unchanged.
- Handles Complex Objects: Supports cloning objects with nested or mutable fields.
When to Use Deep Copy
- When your object contains nested objects.
- When you need to ensure complete data independence between the original and the copied object as shared references can cause unexpected side effects.