Shallow copy vs Deep copy in Java

Copying is the duplication of data/information from source object to a destination object. This is done to avoid the time spent in creating the data once again. Most often it will be required that, after the copy process, any change to the destination object should not impact the source object.

In Java, copying the properties of one object into another can be achieved with the help of cloning process. The java.lang.Object class provides a method called clone() to achieve this. Not all the objects in java are eligible for cloning process. The objects which implement Cloneable interface are only eligible for cloning.

Shallow copy:

Shallow copy is the process in which the fields of primitive types in an object are copied as values whereas if any of the fields in the object is a reference to another object, then, only the memory address pointed by the reference is copied and not the values.

In this case any change made to the destination object will also affect the source object and vice-versa. It should be noted that any change to a String field in destination object will not impact source object. This is because string fields are immutable in Java.

Let us see this through a Java example.

After running the above program, below is the state of each object in the program.

shallow

It is evident from the above output that the empDept field (which is a reference to department object) in both emp1 and emp2 refers to the same memory address pointed by dept1. Hence even if we change the department object of emp2 it will be reflected in emp1 as well. Also it can be seen that a change to the primitive field empId of emp2 doesn’t impact emp1.

Deep copy:

Deep copy is the process in which all the fields in an object are copied as values. Even if any of the fields in the object is a reference to another object, then those fields will also contain copy of the object and not the same memory address. This ensures that any change made in the destination object doesn’t impact the source object and vice versa.

Let us see an example.

Here is an alternative version of a program which uses serialization to achieve deep copy in Java.

After running the above program, below is the state of each object in the program.

deep

It is evident from the output that changes done to emp2 don’t impact emp1.

Note: I have used Serialization concept to achieve deep copy. All the objects (involved in copy) should be serializable. This approach can be useful if the object being copied has a large number of fields which is a reference to another object. The disadvantage of this method is that Serialization is more expensive in terms of time and resource and if your code is performance critical then don’t for this approach. One more thing is that the transient variables cannot be serialized. So using this approach you cannot copy the transient variables.

By the way difference between shallow copy and deep copy is a popular Java interview question.

Shallow copy vs Deep copy

Shallow CopyDeep Copy
Cloned Object and original object are not 100% independent.Cloned Object and original object are 100% independent.
Changes made to cloned object will also be reflected in original object and vice versa.Changes made to cloned object will not be reflected in original object and vice versa.
Default version of clone method can be used to create shallow copy of an object.To create a deep copy of an object, you can implement your own method or you can override clone method and implement clone for each of the reference field.
Shallow copy is preferred if an object has only primitive fields.Deep copy is preferred if an object has references to many other objects as fields.
Shallow copy is fast and also less expensive.Deep copy is slow and expensive.

 

Thanks for reading. If you have any doubt or find any error, post it in the comments section.

The following two tabs change content below.
Working as a Java developer since 2010. Passionate about programming in Java. I am a part time blogger.

Add Comment

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