0% found this document useful (0 votes)
8 views10 pages

Deep Copy & Shallow

Uploaded by

hinatahyuga56890
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views10 pages

Deep Copy & Shallow

Uploaded by

hinatahyuga56890
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Shallow Copy & Deep

Copy
Copy Constructor
• An overloaded constructor.
• When an object is passed to a function, a bitwise (exact)
copy of that object is made and given to the function.
• If the object contains a pointer to allocated memory, the
copy will point to the memory as does the original object.
Copy Constructor (Cont..)
• If the copy makes a change to the contents of this
memory, it will be changed for the original object too.
• Also, when the function terminates, the copy will be
destroyed, causing its destructor to be called.
• That can free dynamically allocated memory, used by the
original object as well.
Shallow Copy
• clone() method of the object class supports a shallow copy of the
object.
• 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.
• If only primitive data type fields or Immutable objects are there, then
there is no difference between shallow and deep copy in Java, That’s
why the name shallow copy or shallow cloning in Java.
Shallow Copy - Example
• public class Person {
private Name name;
private Address address;
public Person(Person originalPerson) {
this.name = originalPerson.name;
this.address = originalPerson.address;
}
[…]
}
Deep Copy

• A deep copy means actually creating a new array and


copying over the values.
• A deep copy is a fully independent copy of an object.
• To create a true deep copy, we need to keep copying all of
a Class object’s nested elements, until there are only
primitive types and “Immutables” left.
Deep Copy - Example
• public class Person {
private Name name;
private Address address;
public Person(Person otherPerson) {
this.name = new Name(otherPerson.name);
this.address = new Address(otherPerson.address);
}
[…]
}
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.
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 as
well as objet references.
• There is no hard and fast rule as to when to do shallow
copy and when to do a deep copy.
Any Question?

You might also like