In Shallow copy, a copy of the original object is stored and only the reference address is finally copied.In Deep copy, the copy of the original object and the repetitive copies both are stored.
Shallow Copy
A shallow copy of an object is a new object whose fields are the same as the original object. However, if the field is a reference to an object, the reference is copied, but the referred object is not. Consequently, both the original object and the shallow copy will reference the same nested objects.
Characteristics of Shallow Copy:
Field-by-Field Copying: The primitive fields (like int,
char, etc.) are copied directly.
Shared References: For object fields, only the reference is copied, not the object itself. Therefore, changes to the referred objects affect both the original and the copied objects.
Default Behavior: The default clone method in Java's Object the class performs a shallow copy.
Example:
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;
}
protected Object clone() throws CloneNotSupportedException {
return super.clone(); // Shallow copy
}
}
public class ShallowCopyExample {
public static void main(String[] args) throws CloneNotSupportedException {
Address address = new Address("New York");
Person person1 = new Person("John", address);
Person person2 = (Person) person1.clone();
System.out.println(person1.address.city); // Output: New York
System.out.println(person2.address.city); // Output: New York
person2.address.city = "Los Angeles";
System.out.println(person1.address.city); // Output: Los Angeles
System.out.println(person2.address.city); // Output: Los Angeles
}
}
Deep Copy
A deep copy of an object involves creating a new object and then recursively copying all objects referenced by the original object. Consequently, changes to the objects referenced by the original object do not affect the deep copy.
Characteristics of Deep Copy:
Independent Copies: All objects referenced by the original object are also copied, resulting in a completely independent copy.
Recursive Copying: Requires implementing cloning for each referenced object to ensure complete independence.
Custom Implementation: Java does not provide a default deep copy method, so it must be implemented manually.
Example:
class Address implements Cloneable {
String city;
Address(String city) {
this.city = city;
}
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Person implements Cloneable {
String name;
Address address;
Person(String name, Address address) {
this.name = name;
this.address = address;
}
protected Object clone() throws CloneNotSupportedException {
Person cloned = (Person) super.clone();
cloned.address = (Address) address.clone(); // Deep copy
return cloned;
}
}
public class DeepCopyExample {
public static void main(String[] args) throws CloneNotSupportedException {
Address address = new Address("New York");
Person person1 = new Person("John", address);
Person person2 = (Person) person1.clone();
System.out.println(person1.address.city); // Output: New York
System.out.println(person2.address.city); // Output: New York
person2.address.city = "Los Angeles";
System.out.println(person1.address.city); // Output: New York
System.out.println(person2.address.city); // Output: Los Angeles
}
}
Summary
Shallow Copy: Only copies primitive fields and references to objects, leading to shared references.
Deep Copy: Recursively copies all objects, resulting in completely independent copies.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Shallow Copy
A shallow copy of an object is a new object whose fields are the same as the original object. However, if the field is a reference to an object, the reference is copied, but the referred object is not. Consequently, both the original object and the shallow copy will reference the same nested objects.
Characteristics of Shallow Copy:
int,char, etc.) are copied directly.Objectthe class performs a shallow copy.Example:
Deep Copy
A deep copy of an object involves creating a new object and then recursively copying all objects referenced by the original object. Consequently, changes to the objects referenced by the original object do not affect the deep copy.
Characteristics of Deep Copy:
Example:
Summary
Read more
What is the difference between JDK, JRE, and JVM?
Bitwise AND, OR, XOR, and left/right shift operations.