Shallow vs Deep Copy (with examples)

Car make: BMW engine: gearbox: Gearbox gears: 6 Engine cylinders: 12 Shallow copy of Car object Car make: BMW engine: gearbox:
Car make: BMW engine: gearbox: Gearbox gears: 6 Engine cylinders: 12 Deep copy of Car object Car make: BMW engine: gearbox: Gearbox gears: 6 Engine cylinders: 12

Arrays

The above example showed an object graph. Same story for arrays.

Shallow copy of array Person age: 30
Person age: 30 Deep copy of array Person age: 30

If there are no children?

Since the concept of deep vs shallow concerns treatment of children, it’s meaningless to talk about deep vs shallow copying of objects (or arrays) that doesn’t have any children. When creating a copy of an array of integers for example, the copy can be seen as both shallow (no children were copied) and deep (all children were copied).

What about pointerA = pointerB?

When you do pointerA = pointerB no object is copied, so it’s neither a deep nor a shallow copy. A pointer value is copied but the value itself (the address) can’t possibly have any children so it doesn’t make sense to try to categorize this copying as shallow or deep.

Mixed copies

Mixed copies are part shallow, part deep. If you for instance make shallow copies of the immediate children, then the original and the copy may share the same grand children. You could say that the copying is one level deep.

Another type of mixed copy would be if you create copies of some children, while sharing others. If you for example have a reference to a singleton, or an immutable object it doesn’t make sense to clone these objects.

When to use which?

This has to be decided on a case by case basis. Here are a few pro’s and con’s of each approach to help you decide.

Comments

Be the first to comment!