• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

CLONE

 
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is shallow clonning and deep clonning?
How do I implement deep clonning and shalow clonning? :roll: :roll:
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Example


if we have shallow clone of instance a1 - a2 it will have values
{ sb1, 1 }, where sb1 the reference to the same instance of StringBuffer,
that has in a1.

So if you now change the StringBuffer in a1, it will be changed also
in a2.

If it is deep clone of a1 - a3, it will have other instance of StringBuffer
(with the same saved value "abcd"). So now if you change StringBuffer in
a1, a2 still have the same value "abcd".

By default java make shallow clone.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think of a data structure as a tree. A shallow clone clones the root of the tree and shares the subtrees of the original tree's root. A deep clone is a recursive copy, i.e. all data structure components in the clone are copies of those in the original. In this case every non-leaf node in the tree would be a copy of the corresponding node in the original. Thus the name shallow vs deep.

Btw this terminology comes from LISP where you could do shallow and deep copies of lists. In this case the shallow copy copied only the top-level of the original list and the deep copy all levels of same.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that this topic has been discussed a few times around these parts before. If you're interested in reading a few more explanations, I'd suggest trying a quick search on this and the intermediate forums. (The search page link is at the top right of this page.)
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic