posted 23 years ago
Hi Angela,
A deep copy is one that results in a totally separate object in memory that has the exact same state as the original. It kind of performs a 'new' function and populate the newer object with the state of the original. I can't give you a good example, because I don't know which class implements cloneable and performs a deep copy.
A shallow copy is one that results in some objects in memory being shared between the new and original objects. A good example of this would be the vector class.
<br /> When the above code is completed we have in memory:<br /> v1.1 --> 0
v1.2 --> 1
v1.3 --> 2
v1.4 --> 3
Now we can perform a cloning: v2 = v1.clone();
This results in the following (in memory):
v1.1 --> 0 <-- v2.1<br /> v1.2 --> 1 <-- v2.2<br /> v1.3 --> 2 <-- v2.3<br /> v1.4 --> 3 <-- v2.4
In this case, Integer is immutable so it won't be a big problem. But if the objects being referenced where not immutable, then if we change the state of v1.1 we would also be changing the state of v2.1!
Regards,
Manfred.