Thursday, 22 September 2011

Shallow Copy and Deep Copy

There are two ways to make a copy of an object called shallow copy and deep copy.
Here is what they mean:

Shallow Copy
Shallow copy is a bit-wise copy of an object. A new object is created that has an exact copy of the values in the original object. If any of the fields of the object are references to other objects, just the references are copied. Thus, if the object you are copying contains references to yet other objects, a shallow copy refers to the same subobjects.

Deep Copy
Deep copy is a complete duplicate copy of an object. If an object has references to other objects, complete new copies of those objects are also made. A deep copy generates a copy not only of the primitive values of the original object, but copies of all subobjects as well, all the way to the bottom. If you need a true, complete copy of the original object, then you will need to implement a full deep copy for the object.
Shallow Copy
Shallow Copy
Deep Copy
Deep Copy

You may like : 
Design patterns using shallow copy and deep copy - Prototype pattern
Shallow and deep copy using clone

No comments:

Post a Comment