Shallow, Deep, And Lazy Copy — Java Basics

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

Rakshit Shah
BeingCoders

--

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.

Shallow Copy

  • 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.
  • 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…

--

--

Rakshit Shah
BeingCoders

Computer Engineer | Foodie | Traveler| Love to learn & earn new things in daily life.