Skip to main content

Programming

Deep Copy and Shallow Copy

·141 words·1 min
Shallow Copy # Copies as little as possible. A new structure created by a shallow copy has the same structure as the old one, and they share the memory address of elements. For example, in Java: int[] arr1 = {1, 2, 3}; int[] arr2 = arr1; arr2 is a shallow copy of arr1. If one of the structures modifies an element, the other will also be affected. Deep Copy # Copies everything. A new structure created by a deep copy not only has the same structure as the old one, but also copies all elements of the old structure to the new memory address.