Shallow VS Deep Copy

From CSSEMediaWiki
Jump to: navigation, search

Probably a very easy way to explain the differences between those two types of copy is to think of a collection copy example. In the case of a shallow copy, we will get a new copy of the collection object itself (for example a new ArrayList instance) but the objects contained inside this collection won't be new copies. The new collection object would be still referring to the same instance objects contained in the original collection (same addresses). We can think of this type of copy as One-Level copy operation.

On the other hand, a deep copy of the collection would give us a new collection object where the contents are all new instance objects with identical properties (...etc) to those objects in the original collection. We can think of this type of copy as All-Levels copy. This indicates that no matter how deep we go with objects containing references of objects containing in turn references of other objects, still the final result should give us all pure new objects that are identical to the original ones.

See Also

Object Copy entry in Wikipedia.

Personal tools