Understanding Shallow and Deep Cloning in Java
What is Cloning?
Before diving into the specifics of shallow and deep cloning, it's essential to understand what cloning entails. In Java, cloning an object means creating a new instance that is a copy of the original object. This can be accomplished using the Cloneable interface and the clone() method provided by the Object class.
The Cloneable Interface
To enable cloning, a class must implement the Cloneable interface. If a class does not implement this interface, calling the clone() method will throw a CloneNotSupportedException.
public class MyClass implements Cloneable {
// Class properties and methods
}
Shallow Cloning
Shallow cloning creates a new object that is a copy of the original object, but it does not create copies of the objects referenced by the original object.
Instead, it copies the references to these objects.
As a result, both the original and cloned objects point to the same instances of the referenced objects.
Example of Shallow Cloning
class Address {
String city;
public Address(String city) {
this.city = city;
}
}
class Person implements Cloneable {
String name;
Address address;
public Person(String name, Address address) {
this.name = name;
this.address = address;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class Main {
public static void main(String[] args) throws CloneNotSupportedException {
Address address = new Address("Indore");
Person original = new Person("Gopal", address);
Person cloned = (Person) original.clone();
System.out.println(original.address.city); // Outputs: Indore
cloned.address.city = "Bhopal";
System.out.println(original.address.city); // Outputs: Bhopal
}
}
Deep Cloning
Deep cloning, on the other hand, creates a new object and recursively clones all the objects referenced by the original object.
This means that the cloned object and the original object do not share any references.
they are entirely independent of each other.
领英推荐
Example of Deep Cloning
To implement deep cloning, we usually need to explicitly clone all mutable fields that are references to other objects. Both classes should implement the Cloneable interface for deep cloning.
class Address implements Cloneable {
String city;
public Address(String city) {
this.city = city;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Person implements Cloneable {
String name;
Address address;
public Person(String name, Address address) {
this.name = name;
this.address = address;
}
@Override
protected Object clone() throws CloneNotSupportedException {
Person cloned = (Person) super.clone();
cloned.address = (Address) address.clone(); // Deep clone of address
return cloned;
}
}
public class Main {
public static void main(String[] args) throws CloneNotSupportedException {
Address address = new Address("Indore");
Person original = new Person("Gopal", address);
Person cloned = (Person) original.clone();
System.out.println(original.address.city); // Outputs: Indore
cloned.address.city = "Bhopal";
System.out.println(original.address.city); // Outputs: Indore
}
}
When to Use Shallow vs. Deep Cloning
Shallow Cloning:
Use shallow cloning when you want to create a copy of an object, and it's acceptable for the original and cloned objects to share references to mutable objects.
This can save memory and improve performance in cases where you are working with immutable or stateless objects.
Deep Cloning:
Use deep cloning when you need to ensure that the cloned object is completely independent of the original.
This is essential when dealing with mutable objects that should not share a state. It will create a new instance for the referenced objects that will use more memory as compared to shallow copy.
Shallow cloning provides a quick way to duplicate objects but can lead to unintended side effects due to shared references. Deep cloning offers a safer alternative at the cost of increased complexity and resource usage. By knowing when to use each type, developers can write more reliable and maintainable code.
#java #springboot #javaprogramming #shallowcloning #deepcloning #cloninginjava
Self-Employed
5 个月Very informative !!!