Interview Questions

What is Shallow copy?

Java Interview Questions for Cloning and Cloneable


(Continued from previous question...)

What is Shallow copy?

In shallow copy the object is copied without its contained objects.
Shallow clone only copies the top level structure of the object not the lower levels.
It is an exact bit copy of all the attributes.
Original
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.
Shallow Copy
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

(Continued on next question...)

Other Interview Questions