Difference between deep copy and shallow copy | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Difference between deep copy and shallow copy

22nd Jul 2017, 7:39 PM
praveen
praveen - avatar
2 Answers
+ 2
deep copy -> All values are being copied over. I'm copying to a different object. Hence the object itself is copied. Shallow copy -> The reference of the object is being copied to a different variable. Since you never specified the language, this example will be with Java. Example/ int[] array = {5,3,1}; // An array is an object int[] temp = array; // SHALLOW copy temp[0] = 10; // This changed the array 'array'. // temp is pointing to the same array, so both are modified. //DEEP COPY int[] deep = new int[array.length]; for(int i = 0; i < deep.length; ++i) deep[i] = array[i]; // All values were copied over, however this is a brand new array. This means changing a value of the array 'deep' will not also change the value of the array 'array'.
22nd Jul 2017, 8:34 PM
Rrestoring faith
Rrestoring faith - avatar
0
Here is a good explanation on something other than a primitive type. http://javaconceptoftheday.com/difference-between-shallow-copy-vs-deep-copy-in-java/
22nd Jul 2017, 10:01 PM
ChaoticDawg
ChaoticDawg - avatar