This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes about clone Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "about clone" Watch "about clone" New topic
Author

about clone

robert chen
Greenhorn

Joined: Oct 19, 2003
Posts: 5
Hi everyone.
My problem is
1.What is the different from deep and shallow copy.
2.What is the example.
Thanks.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
A shallow copy just copies all mutable object references as is. A deep copy clones every object so that the references refer to different objects.
Shallow copy:
Shallow copy example

Deep copy example

Notice that in the deep copy I created a new double[] and copied the contents of it into my clone and I created a new Map with HashMap's copy constructor. Note that on the primitive int and the String member that there is no difference in the clone methods. The reason for this is that primitive instance variables of objects of the same class always have unique storage for each object. As for the String member, Strings are immutable so there is no danger that the cloned object could change the value of the String in the parent.


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
robert chen
Greenhorn

Joined: Oct 19, 2003
Posts: 5
ok.
Thanks for your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about clone
 
Similar Threads
Unregistered?
World Market
WA #2 ..... word association
I Always Wanna Post Java Related Topics
Software Life