Forums Register Login

Deep copy vs. shallow copy

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
Patrick,
Suppose you have the following class.


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.
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send

[ January 30, 2008: Message edited by: Nicholas Jordan ]
My previous laptop never exploded like that. Read this tiny ad while I sweep up the shards.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1419 times.
Similar Threads
shallow copy vs deep copy?
copies of objects
Does arraycopy create aliases?
shallow copy and deep copy?
what is the use of Clonable interface?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 03:02:39.