Shallow vs Deep Copy | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Shallow vs Deep Copy

Hey guys, I read a lot about this but I dont understand it .. Can anyone please explain this in an easy way? would be very grateful because tomorrow I have an exam and I need it.

10th Oct 2017, 6:09 PM
Elsayed Salama
Elsayed Salama - avatar
1 Answer
+ 6
Shallow Copy In Java : The default version of clone() method creates the shallow copy of an object. The shallow copy of an object will have exact copy of all the fields of original object. If original object has any references to other objects as fields, then only references of those objects are copied into clone object, copy of those objects are not created. That means any changes made to those objects through clone object will be reflected in original object or vice-versa. Shallow copy is not 100% disjoint from original object. Shallow copy is not 100% independent of original object. Deep Copy In Java : Deep copy of an object will have exact copy of all the fields of original object just like shallow copy. But in additional, if original object has any references to other objects as fields, then copy of those objects are also created by calling clone() method on them. That means clone object and original object will be 100% disjoint. They will be 100% independent of each other. Any changes made to clone object will not be reflected in original object or vice-versa.
10th Oct 2017, 8:51 PM
Scooby
Scooby - avatar