Ask your own question, for FREE!
Computer Science 13 Online
OpenStudy (shaik0124):

can u please explain what is the difference between deep copy and shallow copy in java? Explain the method

OpenStudy (shaik0124):

@Compassionate @dan815 @ganeshie8

OpenStudy (maitre_kaio):

Let me introduce some concepts before answering your question: An object can have references to other object. For example, consider this: public class Student { private int age; private String name; private Teacher preferredTeacher; } student and name are primitive properties (String can be considered as primitive for our purpose), but preferredTeacher is a reference to a Teacher object. When you do a shallow copy of an object, you're creating a new object (a new Student), but references to other objects remains the same. So the Teacher object referenced by the Student and its shallow copy will be the same. The consequence is that if you change the Teacher in the original, you will change it in its copy too. When you deep copy an object, you make a copy of your object and of the objects its references, recursively. In our example, you will have a new Student object and a new Teacher object. So you can update the Teacher of your first student without updating the second one. The primitive properties, on the other hand, are always duplicated in the copy so you won't have this kind of trouble.

OpenStudy (shaik0124):

i got ur point @maitre_kaio can u please explain with an example how deep copy works i understood the concept but coding point i didn't so can u plz help

OpenStudy (maitre_kaio):

Let's suppose that you want to deep copy Student. We could write this method inside the Student class: public Student deepCopy() { Student copy = new Student(); copy.setAge(this.age); copy.setName(this.name); Teacher teacher = new Teacher(); // set the attributes of teacher the same way we did for student // ...... copy.setTeacher(teacher); return copy; }

OpenStudy (e.mccormick):

In c++ there is the concept of the pointer. This is where something is not the data but instead it just points at it. |dw:1422042520859:dw| The pointer seems like a second copy of the array but it is really just pointing at it. This can also be called a shallow copy of the array. It is not a full copy, but just a copy of how to access the same data. Now, what about: |dw:1422042664375:dw| In this example, both array 1 and array 2 have the same data. If array 2 was made by doing an exact copy of array 1, then it would be a deep copy. In this example, later changes to array 2 would NOT change array 1. This is an important difference between the earlier shallow and the deep copy. In a shallow, changing the copy is really changing the original. In deep, they are seperate items.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!