01 logo

Java Concepts: Important of Shallow, Deep, And Lazy Copy

Let’s explore Java with an example of Shallow copy, deep copy, lazy copy.

By Rakshit ShahPublished 2 years ago 3 min read
2
Shallow, Deep and Lazy copy — java example, Image by Author

In object-oriented programming, object copying is creating a copy of an existing object, the resulting object is called an object copy or simply a copy of the original object. There are several ways to copy an object, most commonly by a copy constructor or cloning.

We can define Cloning as “create a copy of object” Shallow, Deep, and Lazy copy is related to the cloning process these are actually the ways for creating a copy object.

Copying object examples | Image by Author

Shallow Copy

  1. clone() method of the object class supports a shallow copy of the object. If the object contains primitive as well as nonprimitive or reference type variables in shallow copy, the cloned object also refers to the same object to which the original object refers as only the object references get copied and not the referred objects themselves.
  2. Whenever we use the default implementation of the clone method we get a shallow copy of the object means it creates a new instance and copies all the field of the object to that new instance and returns it as an object type, we need to explicitly cast it back to our original object. This is a shallow copy of the object.
  3. That’s why the name shallow copy or shallow cloning in Java. If only primitive data type fields or Immutable objects are there, then there is no difference between shallow and deep copy in Java; Let’s see the Shallow copy example.

The above code shows shallow copying. Here data simply refers to the same array as values; check values and values-data example of a shallow copy.

Shallow copy example with picture

This can lead to unpleasant side effects if the elements of values are changed via some other reference.

Shallow example | Image by Author

Deep Copy

  • Whenever we need our own copy not to use default implementation we call it is Deep copy, whenever we need a Deep copy of the object we need to implement it according to our need.
  • So for a deep copy, we need to ensure all the member classes also implement the Cloneable interface and override the clone() method of the object class.

A deep copy means actually creating a new array and copying over the values.

Deep copy example in java | Image by Author
Deep copy example | Image by Author
Deep copy uses example in java | Image by Author

Changes to the array values will not result in changes to the array data.

When to use what?

There is no hard and fast rule defined for selecting between shallow copy and deep copy but normally we should keep in mind that if an object has only primitive fields, then obviously we should go for shallow copy, but if the object has references to other objects, then based on the requirement, shallow copy or deep copy should be done. If the references are not updated then there is no point to initiate a deep copy.

Lazy Copy

A lazy copy can be defined as a combination of both shallow copy and deep copy. The mechanism follows a simple approach — at the initial state, a shallow copy approach is used. A counter is also used to keep track of how many objects share the data. When the program wants to modify the original object, it checks whether the object is shared or not. If the object is shared, then the deep copy mechanism is initiated.

Summary

In shallow copy, only fields of the primitive data type are copied while the objects’ references are not copied. Deep copy involves the copy of primitive data types similar to object references. There is no hard and fast rule as to when to do shallow copy and when to do a deep copy. Lazy copy is a combination of both of these approaches.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Get my stories in your feeds by subscribing to me, or become a vocal+ member to read all stories of thousands of other writers, participate in all challenges and get a payout with low fees and less payout threshold on Vocal Media.

© Originally published at Java basic fundamentals, also republished on Medium by Rakshit Shah.

apps
2

About the Creator

Rakshit Shah

I am Computer Engineer and love to make websites and software. I am really eager to know about anything. I am curious to read and write cool stuff.

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2024 Creatd, Inc. All Rights Reserved.