What are the differences between a shallow clone of an object and a deep clone?

John Zukowski

By default, cloning yields a shallow copy. By shallow, we mean the object is copied, but the contained objects are not. What a deep copy does it copy the cloned object, as well as all the objects within.

For instance, if you have an array of five elements, you have a total of six objects in memory, the array and the five elements. If you make a shallow copy, you have a total of seven objects in memory: the original array, the copied array, and the set of five elemenets that both arrays contain. For a deep copy, there would be a total of twelve elements since the five elements inside the array will be copied, too. Of course, if each of those elements contained even more objects, then the resulting set of objects would grow even larger.