In general, when we try to copy one object to another object, both the objects will share the same memory address. Normally, we use assignment operator, = , to copy the reference, not the object except when there is value type field. This operator will always copy the reference, not the actual object. For Example: Suppose G1 refers to memory address 5000 then G2 will also refer to 5000. So if one will change the value of data stored at address 5000 then both G1 and G2 will show the same data.
Geeks G1 = new Geeks(); // Copy the instance using // '=' operator Geeks G2 = G1;
Shallow Copy: Creating a new object and then copying the value type fields of the current object to the new object. But when the data is reference type, then the only reference is copied but not the referred object itself. Therefore the original and clone refer to the same object. The concept will more clear when you will see the code and the diagram of the Shallow copy.
Note: In the above diagram The
Global Rank is value type field so it creates the copy of that and store it in different location but the Name
(Desc) is Reference type field so it’s pointing to the old or main memory location.
Example: Here, if you will change value type then it is working on c2 and c1, and not affected but in reference type any change will affect both c1 and c2.
Before Changing: 548 548 GeeksforGeeks GeeksforGeeks After Changing: 548 59 GFG GFG
Deep Copy: It is a process of creating a new object and then copying the fields of the current object to the newly created object to make a complete copy of the internal reference types. If the specified field is a value type, then a bit-by-bit copy of the field will be performed. If the specified field is a reference type, then a new copy of the referred object is performed.
Note:In the above diagram
The Global Rank is value type field so it creates the copy of that and store it in different location. The Name(
Desc) is Reference type field so in Deep Copy there is a clone of reference type field which also will be stored in a different location.
Example: Here, the field type does not matter whether it is value type or reference type. Deep copy makes a copy of whole data and store it in a different memory location so if we change c2, c1 will not affected vice versa also.
Before Changing: 548 548 GeeksforGeeks GeeksforGeeks After Changing: 548 59 GFG GeeksforGeeks
- How to create a shallow copy of SortedList Object in C#
- How to create a shallow copy of Hashtable in C#
- How to create a shallow copy of ArrayList in C#
- How to create a shallow copy of BitArray in C#
- C# | How to create a shallow copy of the BitArray
- Difference Between Machine Learning and Deep Learning
- Introduction to Multi-Task Learning(MTL) for Deep Learning
- ML | Natural Language Processing using Deep Learning
- Difference Between Artificial Intelligence vs Machine Learning vs Deep Learning
- Form N-copy string with add, remove and append operations
- How to disable Copy, Paste, Cut and Right Click using jQuery ?
- How to Enable Copy and Paste in Oracle VirtualBox?
- Copy an Array by Value and Reference into Another Array in Golang
- How to Copy Struct Type Using Value and Pointer Reference in Golang?
- Copy the entire contents of a directory to another directory in PHP
- C# | Copy() Method
- C# | Copy StringCollection at the specified index of array
- C# | Copy the Stack to an Array
- C# | Copy the elements of a string array to the end of the StringCollection
- C# | Copy StringDictionary to Array at the specified index
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.