Trusted answers to developer questions

The distinction between shallow and deep copies of a Java object

Free System Design Interview Course

Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2024 with this popular free course.

What is a copy?

When we copy a primitive value from one variable to another, a duplicate value is created. For example, the following statements

int data = 2;
int copyOfData = data;

result in two variables that each contain the integer 2. This is illustrated below.

What happens when variables represent objects? Well, such a variable will contains a reference to the object. For example, the statements

String bug = "spider";
String insect = bug;

create two variables that contain the same reference to the string "spider". In fact, bug and insect are alisases, as pictured here:

A variable and its alias, both of which reference the same object

An alias does not reference a copy of an object. So, what happens when we duplicate an object?

Shallow copies

In the following illustration, the variable anObject references an object, and the variable anObjectCopy references a duplicate of the object.

A shallow copy of an object whose data field is another object
A shallow copy of an object whose data field is another object

The original object has a data field that references a different object. The data field and its reference are duplicated in the object’s copy. This duplicate reference is an alias; thus, the original object and its copy both reference the same piece of data. For this reason, the copy is known as a shallow copy.

Note: An object’s shallow copy

A duplicate of an object that shares some or all of its component objects with the original object is known as a shallow copy.

Deep copies

We again consider the object referenced by the variable anObject and its copy that the variable anObjectCopy references. However, this time, rather than duplicating the reference in the object’s data field, we duplicate the object the data field references. The following illustration depicts these objects. We now have a deep copy of the original object.

A deep copy of an object whose data field is another object
A deep copy of an object whose data field is another object

Note: An object’s deep copy

An exact duplicate of an object that also duplicates all of its component objects is known as a deep copy. This duplicate is completely distinct from the original object. A clone in Java is the same as a deep copy. When you copy an object that can be modified, perhaps by a set method, the copy should be a deep copy.

Copies of arrays

In Java, an array is an object. Arrays can contain primitive values or references to objects. So, what happens when we make a copy of an array?

When we duplicate an array of primitive values, such as integers, we get two arrays that each contain the same primitive values, as shown here:

An array of primitives and its copy
An array of primitives and its copy

An array of objects contains only references to the objects, not the objects themselves. Thus, copying such an array copies the references.

If we duplicated an array of strings, for example, we would get two distinct arrays, as shown below:

An array of strings and its shallow copy
An array of strings and its shallow copy

The references, but not the strings, are copied from one array to the other. Thus, each array would contain its own references to the strings, but there would only be one set of strings. Thus, the copy of the array is a shallow copy.

Note: Since any string, once created, cannot be altered, sharing it causes no problems. Thus, a shallow copy of an array of strings is perfectly acceptable.

If we were to create duplicates of both the array and the strings, the original array and the duplicate array would each reference their own set of strings. This duplicate array would be a deep copy or a clone.

A array of strings and its deep copy
A array of strings and its deep copy

RELATED TAGS

java
Did you find this helpful?