Csc.216/s08/cultivate virtue
Deep Copies vs. Shallow Copies in Java Active Learning Exercise 1 Mark Etheridge Nick Forrer
In object oriented languages, it is often necessary to copy (or clone) an entire object, including all of the object's member variables. Unfortunately, it is not enough to simply say object1 = object2, since this will likely only copy a reference to the second object (unless there has been some sort of operator overloading). It is necessary, therefore, to copy each of the member variables individually. But be careful! Just as in the original example, it is often not enough to simply use the assignment operator, as this will likely only reference the second object, instead of actually copying the memory. Such an occurrence is known as a shallow copy, and can be the source of much grief if not properly accounted for. The solution to this phenomenon is called a deep copy, and simply requires the allocation of new memory for each and every member variable in the class being copied.