Wednesday, September 19, 2007

Shallow and Deep Copy

We generally clone an object using java.lang.Cloneable.clone(). This method makes a shallow copy and not a deep copy.

Difference between the two can be understood by using following example:


public class CloningExample implements Cloneable

{
private LinkedList names = new LinkedList();

public CloningExample()
{

names.add("abc");

names.add("xyz");
}


public Object deepClone() throws CloneNotSupportedException
{
CloningExample copy = (CloningExample)super.clone();

copy.names = (LinkedList)names.clone();

return copy;
}

public boolean equals(Object obj)
{
CloningExample tmp = (CloningExample)obj;

if (this.names == tmp.names)

return true;

else

return false;
}

public static void main(String[] args) throws CloneNotSupportedException
{
CloningExample ce1 = new CloningExample();

System.out.println("\nCloningExample[1] ----------------- " + ce1.names);

CloningExample ce2 = (CloningExample)ce1.clone();

System.out.println("\nCloningExample[2] ----------------- " + ce2.names);

System.out.println("\nCompare Shallow Copy --------------------\n" +

" ce1 == ce2 : " + (ce1 == ce2) + "\n" +

" ce1.equals(ce2) : " + ce1.equals(ce2));

CloningExample ce3 = (CloningExample)ce1.deepClone();

System.out.println("\nCompare Deep Copy --------------------\n" +

" ce1 == ce3 : " + (ce1 == ce3) + "\n" +

" ce1.equals(ce3) : " + ce1.equals(ce3));
}

}


Shallow Copy

CloningExample ce2 = (CloningExample)ce1.clone();

A clone ce2 of ce1 is created by simply calling the clone() method of Object. Our overridden equals() method tells us that the "names" are equivalent, but operator == tells that they aren't equal - that is, that are two distinct objects. This is what referred as a "Shallow Copy". Java simply copied the bits of our variables. This means that the "names" instance variable of both objects (ce1 and ce2) still holds the same information - that is, both objects have a reference to the same LinkedList.


Deep Copy

CloningExample ce3 = (CloningExample)ce1.deepClone();

The code above will perform a deep copy of the ce1 to ce3. The first part of the clone in the deep copy is the same as it was for the shallow copy. deepClone() method now

clones the LinkedList as well. Now, when CloningExample object is cloned, each CloningExample object (ce1 and ce3) will have its own LinkedList.

3 comments:

Anonymous said...

I have spent the last year facilitating various master mind groups studying the book Think and Grow Rich and moving into group coaching opportunities which are beginning to really transform our conversations. [url=http://www.mulberryhandbagssale.co.uk]Mulberry outlet shop[/url] The 19th century German chemist named Kekule dreamed of a snake with its tail in its mouth one day after dosing off. [url=http://www.goosecoatsale.ca]canada goose online[/url] Hjnyzkmie
[url=http://www.pandorajewelryvip.co.uk]pandora bracelets[/url] Ngbduukrt [url=http://www.officialcanadagooseparkae.com]canada goose[/url] fksaeaywr

Anonymous said...

I wrote the shows when I was 19, and wanted to see if I had it in me to be able to write manipulative fiction meant for mass consumption. ghd diamond edition world war 1, affected fashion, coal was scarce and women were doing factory jobs that men had to do prior the war. ugg slippers The cast effortless captured the personalities and attitudes of the characters set to play, for an overall wonderful execution of this musical. http://www.oneghdhair.com So I think that it really cool to be able to go to a tailor and say, "Please take my pants in, take in the side seams, cut off this weird bow." Whatever it is. ugg sale Over the last decade, they have playfully sampled and remixed their way through visual codes and media, confronting the viewer head-on, regardless of cultural background..

Anonymous said...

My one-skein wonder is complete, though! It truly was a wonder that I was able to finish it. http://www.lateuggboots.com The most important thing is that it should not be maintained in similar pouches. ugg Create a line as close to your lash line as possible, add a couple dots at the lower outer corners and smudge lightly with the tip of a clean makeup sponge.. ghd purple Some of the first designs that have been sold during this time still exist up to now. http://www.softuggboots.com First, the game does allow for the capture of aliens for interrogation, but the old X-COM concept of capturing multiple alien engineers to learn individual UFO specifications, alien navigators to discover the types of missions the aliens conduct, or alien medics to learn more about the anatomy of different alien races is nowhere to be found in the new X-COM.