Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
17Activity
0 of .
Results for:
No results containing your search query
P. 1
Deep Cloning vs Shallow Cloning

Deep Cloning vs Shallow Cloning

Ratings: (0)|Views: 4,127|Likes:
Published by api-3831588

More info:

Published by: api-3831588 on Oct 18, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

03/18/2014

pdf

text

original

 
Java Tip 76: An alternative tothe deep copy technique
Use serialization to make deep copies and avoidextensive manual editing or extending of classes
Summary
If you ever have a complex object that contains a numberof other objects, and want to implement a
deep copy 
-- acopy of the object and the objects to which it refers -- youknow you're in for a painful and time-consuming task. Thisis because the default behavior of an object's
clone()
method automatically yields a shallow copy (a copy thatexcludes contained objects) -- so, to achieve a deep copy,classes must be manually edited or extended. If an objecthas a complex graph, then this process will result in anextensive coding effort. The technique presented in thisarticle is a simple alternative to this difficult procedure,and takes advantage of functionality provided byserialization.
(2,000 words)
 Implementing a deep copy of an object can be a learning experience --you learn that you don't want to do it! If the object in question refersto other complex objects, which in turn refer to others, then this taskcan be daunting indeed. Traditionally, each class in the object must beindividually inspected and edited to implement the
Cloneable
interfaceand override its
clone()
method in order to make a deep copy of itself as well as its contained objects. This article describes a simpletechnique to use in place of this time-consuming conventional deepcopy.
The concept of deep copy
In order to understand what a
deep copy 
is, let's first look at theconcept of shallow copying.In a previous
 JavaWorld 
article, "How to avoid traps and correctlyoverride methods from java.lang.Object," Mark Roulo explains how toclone objects as well as how to achieve shallow copying instead of deep copying. To summarize briefly here, a shallow copy occurs whenan object is copied without its contained objects. To illustrate, Figure 1shows an object,
obj1
, that contains two objects,
containedObj1
and
containedObj2
.
 
Figure 1. The original state of obj1
If a shallow copy is performed on
obj1
, then it is copied but itscontained 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 towhich it refers. Figure 3 shows
obj1
after a deep copy has beenperformed on it. Not only has
obj1
been copied, but the objectscontained 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 theentire graph is traversed and copied.Each object is responsible for cloning itself via its
clone()
method. Thedefault
clone()
method, inherited from
Object
, makes a shallow copy of the object. To achieve a deep copy, extra logic must be added thatexplicitly calls all contained objects'
clone()
methods, which in turn calltheir contained objects'
clone()
methods, and so on. Getting thiscorrect can be difficult and time consuming, and is rarely fun. To makethings even more complicated, if an object can't be modified directlyand its
clone()
method produces a shallow copy, then the class mustbe extended, the
clone()
method overridden, and this new class usedin place of the old. (For example,
Vector
does not contain the logicnecessary for a deep copy.) And if you want to write code that defersuntil runtime the question of whether to make a deep or shallow copyan object, you're in for an even more complicated situation. In thiscase, there must be two copy functions for each object: one for a deepcopy and one for a shallow. Finally, even if the object being deepcopied contains multiple references to another object, the latter objectshould still only be copied once. This prevents the proliferation of objects, and heads off the special situation in which a circularreference produces an infinite loop of copies.
Serialization
Back in January of 1998,
 JavaWorld 
initiated its
JavaBeans
column byMark Johnson with an article on serialization, "Do it the 'Nescafé' way-- with freeze-dried JavaBeans." To summarize, serialization is theability to turn a graph of objects (including the degenerate case of asingle object) into an array of bytes that can be turned back into an

Activity (17)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
Hasan Hussain liked this
rajaramansuresh liked this
a.giacchetto liked this
rguptamtech liked this
bharat liked this

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->