• 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

Deep copy vs. shallow copy

 
Gunslinger
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so why does it matter? I understand the difference between the two (effectively creating a pointer, versus setting the variables equal), but my book does not state why deep copy may be better. Can anyone provide me with a response?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Patrick,
Suppose you have the following class.


The caller of Copy has a reference to the original list. With a shallow copy, the caller's changes are seen by Copy and Copy's changes are seen by the caller. This can introduce some subtle side effects.
 
James Brooks
Gunslinger
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, are you saying that this would add the item to both lists? Also, is there a functional difference (taking away the syntactic sugar) between Object.getCopy()/Object.makeCopy(), and clone()? Thanks!
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeanne has deep knowledge, so I will leave it to the OO'rs to explain it in OO terminology.

A shallow copy just makes another one, without transferring ( copying ) the values. A deep copy does something along the lines of for each item in source collection, set value in new collection to same as source collection.

This may involve the new operartor, many fundamental classes that are often used allow a reference to same kind as a constructor paramater and result in a totally new object which will not pass-through to the source complex object.

Anything other than a primitive would likely be a complex object, most any instance of a class is.
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[ January 30, 2008: Message edited by: Nicholas Jordan ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic