Shallow Vs Deep Cloning

Shallow Copy.
Shallow copy is a bit-wise copy of an object.
A new object will be created that has an exact copy of the values as it is there in the original object.
If any of the fields of the original object are references to other objects, just the reference addresses are copied.

               deepcopy versus shallow copy

In this figure, the Object A have field One is primitive int type and other is referencing to object B.
When we do a shallow copy of Object A, Copied Object A is created with copied value of int a and still referring to Object B itself.
You will find that since a is primitive data type, values of 'a' got copied,but Object A was referring to Object B so there is no copy for Object B just Copied Object A is also referring to Object B.
So if any thing will be changed in Object B, it will be reflecting in both Object A and Copied Object A.

Deep Cloning.


A deep copy copies all fields.
In deep Cloning an object is copied along with the objects to which it refers.
You can see the difference in compare to Shallow Copy.
Here all the fields has copied including referenced object also.
So if we change something in Object B, it will not impact Copied Object B hence it will not impact to Copied Object A also.