Clone Shallow copy vs Deep Copy in Java With Examples
Table of Contents
Clone Shallow copy vs Deep Copy in Java With Examples:
Clone() method in java used for object duplication. We do not have any method/way other than clone() to make object duplication. Assignment operator(=) will assign the
same reference, but it will not duplicate the objects.
In java clone() is divided into two types namely shallow copy and deep copy.
Shallow copy:
Cloning a new object and copying the same object properties to the cloned object is called as shallow copy. So if we do any changes in any objects it will affect in both the objects. Since two different objects references to the same object properties.
Shallow copy will not call the constructor. It is highly not recommended to use with mutable objects, because shallow clone copy can create data inconsistency.
Clone Shallow copy Java Example:
[java]
package in.javadomain;
public class ShallowCopyExample implements Cloneable {
ShallowCopyExample() {
System.out.println(“Constructor Called!”);
}
public static void main(String[] args) throws CloneNotSupportedException {
// constructor called with new() keyword
ShallowCopyExample shallow = new ShallowCopyExample();
// constructor did not call with clone()
shallow.clone();
}
}
[/java]
Output:
[plain]
Constructor Called!
[/plain]
Deep copy:
Cloning a new object and it’s properties to new memory reference is called deep copy. Here if you make any changes to your original object which will not modify the cloned object, since both the objects refer the differnt memory locations.
Clone Deep copy Java Example:
[java]
package in.javadomain;
public class ShallowCopyExample implements Cloneable {
ShallowCopyExample() {
System.out.println(“Constructor Called!”);
}
public static void main(String[] args) throws CloneNotSupportedException {
// constructor called with new() keyword
ShallowCopyExample shallow = new ShallowCopyExample();
// constructor called with clone() – deep copy.
shallow.clone();
}
public Object clone() {
ShallowCopyExample shallow = new ShallowCopyExample();
return shallow;
}
}
[/java]
Output:
[plain]
Constructor Called!
Constructor Called!
[/plain]
Shallow copy Vs Deep copy – Better for performance ?
As per the below program shallow copy completes it’s execution in around just 500 milli seconds, where as deep copy takes around 1000 milli seconds.
Main reason is shallow copy does not call the constructor, where as deep copy calls constructor, so if you create only two objects then execution speed doubles in deep copy, if three then it triples it’s execution etc..
Shallow Copy – Java Sample Program for performance:
[java]
package in.javadomain;
public class ShallowDeep implements Cloneable {
ShallowDeep() {
for (int i = 0; i < 100000; i++) {
System.out.println(i);
}
}
public static void main(String[] args)
throws InstantiationException, IllegalAccessException, ClassNotFoundException, CloneNotSupportedException {
long startTime = System.currentTimeMillis();
ShallowDeep ivar = new ShallowDeep();
ShallowDeep ivr = (ShallowDeep) ivar.clone();
long endTime = System.currentTimeMillis();
long diff = endTime – startTime;
System.out.println(“Executed in :::” + diff + ” milli seconds”);
}
}
[/java]
Output:
[plain]
Executed in :::470 milli seconds
[/plain]
Deep Copy – Java Sample Program for performance:
[java]
package in.javadomain;
public class ShallowDeep implements Cloneable {
ShallowDeep() {
for (int i = 0; i < 100000; i++) {
System.out.println(i);
}
}
public static void main(String[] args)
throws InstantiationException, IllegalAccessException, ClassNotFoundException, CloneNotSupportedException {
long startTime = System.currentTimeMillis();
ShallowDeep ivar = new ShallowDeep();
ShallowDeep ivr = (ShallowDeep) ivar.clone();
long endTime = System.currentTimeMillis();
long diff = endTime – startTime;
System.out.println(“Executed in :::” + diff + ” milli seconds”);
}
public Object clone() {
// Cloning – deep copy
ShallowDeep s = new ShallowDeep();
return s;
}
}
[/java]
Output:
[plain]
Executed in :::1001 milli seconds
[/plain]
Note: If you directly try to use clone() method without implementing cloneable interface, then you will get “java.lang.CloneNotSupportedException”. So implement
cloneable interface to the class where you want to making clone().