Shallow Copy and Deep Copy in Java

Shallow Copy also means Shallow Clone and Deep Copy also means Deep Clone.

Cloning an object is about creating the copy of the original object, i.e., making an identical copy of the original object.

By default, cloning in Java is field by field copy i.e. as the Object class does not have idea about the structure of class on which clone() method will be invoked. Therefore JVM does the following things when called for cloning:

1) If the class has only primitive data type members then a completely new copy of the object will be created and the reference to the newly copied object will be returned.

2) If the class contains members of any class or object type then only the object references to those members are copied and hence the member references in both the original object as well as the cloned object refer to the same object.

Let’s take an example, we want to copy object A and we get object B from that. Let’s see how it happens in shallow and in deep copy.

Shallow Copy

Shallow copy duplicates as little as possible. A shallow copy of a collection is a copy of the collection structure, not the elements. With a shallow copy, two collections now share the individual elements.

In shallow copy when we copy object A and B object is created. Object B points to the object A’s location in the memory. So the reference pointer is also copied just like the values. Therefore, the reference is pointing to the original object. Any changes to the member that are stored by reference appear in both the original and the copied object, since no copy was made of the referenced object.

A Shallow Copy is similar to Call By Reference where both formal and actual parameters of a function refers to same memory location and the value.

Deep Copy

Deep copy duplicates everything. A deep copy of a collection is two collections with all of the elements in the original collection duplicated.

In deep copy when we copy object A then object B is created. All things in object A’s memory location get copied to object B’s memory location. So the pointer to object stored by reference is not copied. Instead, a deep copy is made of the referenced object, and a pointer to the new object is stored. Any changes that are made to those referenced object will not affect other copies of the object.

A Deep Copy is similar to Call By Value where both formal and actual parameters of a functions refers to different memory location but having the same value.

Example

In Java, if a class needs to support cloning it has to do following things:

It must implement Cloneable interface.
It must override clone() method from Object class.

Shallow Copy

Output 

In the above output we see that after setting the new Employee name to the cloned object the original object gets affected.

Deep Copy

Output 

In the above output we see that after setting the new Employee name to the cloned object the original object does not get affected.

That’s all. Thanks for reading.

Soumitra

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.