Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Cloneableinterface and override its
clone()method in order to make a deep copy of itself as well as its contained objects. This article describes a simple technique to use in place of this time-consuming conventional deep copy.
In order to understand what a deep copy is, let's first look at the concept of shallow copying.
In a previous JavaWorld article, "How to avoid traps and correctly override methods from java.lang.Object," Mark Roulo explains how to clone objects as well as how to achieve shallow copying instead of deep copying. To summarize
briefly here, a shallow copy occurs when an object is copied without its contained objects. To illustrate, Figure 1 shows
obj1, that contains two objects,
Figure 1. The original state of obj1
If a shallow copy is performed on
obj1, then it is copied but its contained objects are not, as shown in Figure 2.
Figure 2. After a shallow copy of obj1
A deep copy occurs when an object is copied along with the objects to which it refers. Figure 3 shows
obj1 after a deep copy has been performed on it. Not only has
obj1 been copied, but the objects contained within it have been copied as well.
Figure 3. After a deep copy of obj1
If either of these contained objects themselves contain objects, then, in a deep copy, those objects are copied as well, and
so on until the entire graph is traversed and copied. Each object is responsible for cloning itself via its
clone() method. The default
clone() method, inherited from
Object, makes a shallow copy of the object. To achieve a deep copy, extra logic must be added that explicitly calls all contained
clone() methods, which in turn call their contained objects'
clone() methods, and so on. Getting this correct can be difficult and time consuming, and is rarely fun. To make things even more
complicated, if an object can't be modified directly and its
clone() method produces a shallow copy, then the class must be extended, the
clone() method overridden, and this new class used in place of the old. (For example,
Vector does not contain the logic necessary for a deep copy.) And if you want to write code that defers until runtime the question
of whether to make a deep or shallow copy an object, you're in for an even more complicated situation. In this case, there
must be two copy functions for each object: one for a deep copy and one for a shallow. Finally, even if the object being deep
copied contains multiple references to another object, the latter object should still only be copied once. This prevents the
proliferation of objects, and heads off the special situation in which a circular reference produces an infinite loop of copies.