OK, so why does it matter? I understand the difference between the two (effectively creating a pointer, versus setting the variables equal), but my book does not state why deep copy may be better. Can anyone provide me with a response?
Jeanne Boyarsky
,
author & internet detective
staff
The caller of Copy has a reference to the original list. With a shallow copy, the caller's changes are seen by Copy and Copy's changes are seen by the caller. This can introduce some subtle side effects.
So, are you saying that this would add the item to both lists? Also, is there a functional difference (taking away the syntactic sugar) between Object.getCopy()/Object.makeCopy(), and clone()? Thanks!
Jeanne has deep knowledge, so I will leave it to the OO'rs to explain it in OO terminology.
A shallow copy just makes another one, without transferring ( copying ) the values. A deep copy does something along the lines of for each item in source collection, set value in new collection to same as source collection.
This may involve the new operartor, many fundamental classes that are often used allow a reference to same kind as a constructor paramater and result in a totally new object which will not pass-through to the source complex object.
Anything other than a primitive would likely be a complex object, most any instance of a class is.