jargon

Comparison

Deep copyvsShallow copy

Deep copy

Defined by the difference stated below.

Shallow copy

Defined by the difference stated below.

The difference

A shallow copy duplicates the object and shares whatever its fields point at; a deep copy duplicates the reachable graph as well. Almost every default copy in every language is shallow, which is why the surprise is so common. Deep copies are expensive, must decide what to do about cycles and shared identity, and are usually a sign that immutable values would serve better.

Entry: Deep copy vs shallow copy

Related comparisons