aspose file tools*
The moose likes Beginning Java and the fly likes What is the difference between the deep copy and shallow copy in Object.clone() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the difference between the deep copy and shallow copy in Object.clone()" Watch "What is the difference between the deep copy and shallow copy in Object.clone()" New topic
Author

What is the difference between the deep copy and shallow copy in Object.clone()

Jasmine kaur
Ranch Hand

Joined: Nov 25, 2003
Posts: 160
Hi Everybody,
Please clear this concept of shallow copy and deep copy of the object when we invoke Object.clone();
Thanks in advance.


jasmine kaur
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225

In cloneShallow, the returned object has a reference to the same object anObject. In cloneDeep, anObjecty is copied.

Object.clone() method performs a shallow copy, so when overriding this method, you should first call super.clone() and then modify some fields to perform the "deep" copy. (And returning a full indepedent object).


Regards,<br />Seb<br /> <br />SCJP 1.4
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5

I like to think of shallow/deep cloning in terms of parents and children. Shallow cloning copies the parent but shares the same children, whereas deeping cloning duplicates parents and children.

For example, if a parent A has 100 children objects (poor mother), then a shallow clone would create a second parent B with references to the exact same children. In total 1 new object reference would be created. So if then modified parent A's children, parent B's children would be affected since they are shared.

With deep cloning, when I created parent B all the 100 children would also be copied. So this would amount to 101 new objects and modifying parent A's children would not affect parent B's children. Think of the performance difference between creating one object versus 101, which is why deep cloning is definitely a more expensive call.

In most situations where you are even addressing cloning, I find you tend to want deep cloning, and have to implement it manually in all classes except where you use primitives (primitives are never shared, objects are always shared in shallow cloning). For example, if I have 1 parent class and multiple children and grandchildren classes, I'd have to write clone method in each and every class. Further, if I have arrays of children (very common) I have to clone the array by creating a new array, then manually cloning the children and adding them to the new array. One advantage, though, is that for cloning children you can delegate to their clone methods, so you never have to deal with more than 2 levels at a given time.


My Blog: Down Home Country Coding with Scott Selikoff
Hung Yee
Greenhorn

Joined: Dec 22, 2004
Posts: 18
FYI: Here are some more examples of 'deep copying' techniques from Sun:
http://java.sun.com/developer/JDCTechTips/2001/tt0410.html#making
Jasmine kaur
Ranch Hand

Joined: Nov 25, 2003
Posts: 160
Thanks alot for responding , well Iam still confused so could u please make this statement clear what it means exactly
"if a parent A has 100 children objects (poor mother)"
then a shallow clone would create a second parent B with references to the exact same children. In total 1 new object reference would be created. So if then modified parent A's children, parent B's children would be affected since they are shared.

I would appreciate your suggestion if u make this statement clear as Iam still preplex.
Thanks in advance.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Jasmine kaur:
Thanks alot for responding , well Iam still confused so could u please make this statement clear what it means exactly
"if a parent A has 100 children objects (poor mother)"
then a shallow clone would create a second parent B with references to the exact same children. In total 1 new object reference would be created. So if then modified parent A's children, parent B's children would be affected since they are shared.

I would appreciate your suggestion if u make this statement clear as Iam still preplex.
Thanks in advance.


I think that is Scott's attempt to add humor to his explanation. The "(poor mother)" is using a pun on the biological relationship beteween parents and children. It's also indicating that 100 children would be a very overwhelming responsibility for a single parent.

To understand the programming concepts, you should ignore the part in parenteses.

Layne
[ October 25, 2005: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

It's better you have a or when someone is joking.


If you are not laughing at yourself, then you just didn't get the joke.
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
Originally posted by Jasmine kaur:

"if a parent A has 100 children objects (poor mother)"


LOL, this is funny. There <i>is</i> an Indian legend in which a couple has 100 children.



ASCII silly question, Get a silly ANSI.
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5

I think that is Scott's attempt to add humor to his explanation. The "(poor mother)" is using a pun on the biological relationship beteween parents and children. It's also indicating that 100 children would be a very overwhelming responsibility for a single parent.


I was trying to encourage mental visualation of the concept so it would be better remembered and understood! Or, I just have terrible sense of humor. Either way, my attempts at humor may doom us all!
[ October 28, 2005: Message edited by: Scott Selikoff ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the difference between the deep copy and shallow copy in Object.clone()