PS: If you like the page or have any questions, feel free to comment at end.
Q1) What are different type of cloning in Java?
Ans) Java supports two type of cloning: - Deep and shallow cloning. By default shallow clone is used in Java. Object class has a method clone() which does shallow cloning.
Q2) What is Shallow copy?
Ans) Shallow clone is a copying the reference pointer to the object, which mean the new object is pointing to the same memory reference of the old object. The memory usage is lower.
Figure 1: Original java object obj
The shallow copy is done for obj and new object obj1 is created but contained objects of obj are not copied.
Figure 2: Shallow copy object obj1
It can be seen that no new objects are created for obj1 and it is referring to the same old contained objects. If either of the containedObj contain any other object no new reference is created.
Q3) What is deep copy and how it can be acheived?
Ans) In deep copy is the copy of object itself. A new memory is allocated for the object and contents are copied.
Figure 3 : Original Object obj
When a deep copy of the object is done new references are created.
Figure 4: obj2 is deep copy of obj1
One solution is to simply implement your own custom method (e.g., deepCopy()) that returns a deep copy of an instance of one of your classes. This may be the best solution if you need a complex mixture of deep and shallow copies for different fields, but has a few significant drawbacks:
Other common solution to the deep copy problem is to use Java Object Serialization (JOS). The idea is simple: Write the object to an array using ObjectOutputStream and then use ObjectInputStream to reconsistute a copy of the object. The result will be a completely distinct object, with completely distinct referenced objects. JOS takes care of all of the details: superclass fields, following object graphs, and handling repeated references to the same object within the graph.
There are ways to speed it up (e.g., by pre-computing serial version ids and defining custom readObject() and writeObject() methods), but this will usually be the primary bottleneck. The byte array stream implementations included in the java.io package are designed to be general enough to perform reasonable well for data of different sizes and to be safe to use in a multi-threaded environment. These characteristics, however, slow down ByteArrayOutputStream and (to a lesser extent) ByteArrayInputStream .
Q4) What is difference between deep and shallow cloning?
Ans) The differences are as follows:
public class MyData{
String id;
Map myData;
}
The shallow copying of this object will be pointing to the same memory reference as the original object. So a change in myData by either original or cloned object will be reflected in other also. But in deep copying there will memory allocated and values assigned to the property will be same. Any change in object will not be reflected in other.
Shallow copying is default cloning in Java which can be achieved using Object.clone() method of Object class. For deep copying override the clone method to create new object and copy its values.
Q5) What are disadvantages of deep cloning ?
Ans) Disadvantages of using Serialization to achieve deep cloning –