Javatpoint Logo
Javatpoint Logo

Shallow Copy Java

In Java, creating a clone or copy of an object is the most important task. In this section, we will discuss what is a shallow copy in Java and how to create a shallow copy of a Java object. Before moving to the shallow copy, first, we will understand what is a copy in Java and the difference between a reference copy and an object copy.

As the name indicates, a reference copy creates a copy of a reference variable pointing to an object. For example, if we have a Byke object, with a myByke variable pointing to it and we make a reference copy, we will now have two myByke variables, but still one object.

An object copy creates a copy of the object itself. So, if we again copied our byke object, we would create a copy of the object itself, as well as a second reference variable referencing that copied object.

Shallow Copy

A shallow copy of an object is a new object whose instance variables are identical to the old object. For example, a shallow copy of a Set has the same members as the old Set and shares objects with the old Set through pointers. Shallow copies are sometimes said to use reference semantics.

Shallow Copy Java

In other words, we can say that in shallow copy only references are copied. Therefore, both original and object point to the same reference. Creating a new reference that points to the same memory location. Note that when we try to alter data in the copied object, the changes are also reflected in the original one.

Shallow Copy Java

When we create a copy of an object using a shallow copy mechanism, all fields of the original objects are copied. On the other hand, if it contains objects as fields, only the references to those objects are copied but not complete objects. A point to remember is that only primitive data types are copied while the object references are not copied.

Shallow Copy Java

Note: The clone() method of the Object class creates a shallow copy, by default.

Consider the following code snippet.

Shallow Copy Java Program


Original (orginal values): Person-A - Civic
Clone (before change): Person-A - Civic
Clone (after change): Person-B - Accord
Original (after clone is modified): Person-A - Accord

Shallow Copy Vs. Deep Copy

Shallow Copy Deep Copy
Cloned Objects and original objects are not completely disjoint. Cloned Objects and original objects are completely disjoint.
Alteration made to cloned object also imitated in an original one or vice versa. Alteration made to cloned object also be imitated in an original object or vice versa.
The default version of the clone() method creates a shallow copy of an object. To create the deep copy of an object, we have to override the clone() method.
Shallow copy is preferred if an object has only primitive fields. Deep copy is preferred if an object has references to other objects as fields.
It is fast and also less expensive. It is slow and very expensive.
Memory is utilized efficiently. Memory is not utilized efficiently.
It is not much error-prone. It is much error-prone.

Next TopicBiFunction Java 8

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA