What are the differences between shallow and deep copies?

  • Thread starter Thread starter Chromium
  • Start date Start date
Click For Summary

Discussion Overview

The discussion centers around the differences between shallow and deep copies in programming, particularly in the context of Java. Participants explore the definitions, implications, and examples of both concepts, seeking clarification and validation of their understanding.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant presents a visual representation of shallow and deep copies, suggesting that a shallow copy involves multiple references pointing to the same object, while a deep copy creates a new object that is distinct but identical in value.
  • Another participant confirms the accuracy of the initial explanation, albeit with some confusion regarding the formatting of the examples.
  • A later reply expresses gratitude for the reassurance that the understanding of deep and shallow copies is correct.
  • Another participant adds that shallow copies can be altered by changes to the source object, while deep copies remain unaffected, implying a potential risk associated with shallow copies.

Areas of Agreement / Disagreement

Participants generally agree on the definitions of shallow and deep copies, but there is some confusion regarding the presentation of the examples. The discussion does not reach a consensus on the best way to illustrate these concepts.

Contextual Notes

Some participants note issues with the formatting of the examples, which may affect clarity. There is also an implied assumption that the audience has a basic understanding of object references in programming.

Chromium
Messages
56
Reaction score
0
Hey Everyone,

Could someone confirm whether or not this "picture" of a shallow and deep copy is accurate? Since I've only programmed in Java, my code/pseudo-code will be java-ish.

Shallow Copy

Object Reference String Object
String s 10010101010

In this case, a String object has a String reference s "pointing" to it.


Object Reference String Object
String s 10010101010
t

In this case, the same String object has two object references pointing to it. In
actuality, t is just a copy of s. So in short, a shallow copy is just a copy of the
object reference. Ultimately, you have two object references pointing to the
same object.

Deep Copy

Object Reference String Object
String s 10010101010

The String object still only has reference pointing to it.

Object Reference String Object
String s 10010101010
String t 10010101010

Now there is a completely new and different String object (however, it is a copy
of the original String object. The new object reference points to a completely
distinct (yet exactly alike) object.



Feel free to criticize this "picture" of shallow v. deep copies if it is vague, unclear,
plain wrong, etc...(but please be gentle!)
 
Last edited:
Technology news on Phys.org
Your table formatting confuses me a little but otherwise yes, that is exactly correct.
 
Sorry about that, I'm not sure why it turned out like that. I went back to edit it, but the tables are still the same, oh well. By the way, thanks for reassuring me that I indeed understand deep and shallow copies!
 
Chromium said:
Sorry about that, I'm not sure why it turned out like that. I went back to edit it, but the tables are still the same, oh well. By the way, thanks for reassuring me that I indeed understand deep and shallow copies!

Wrap your text in [ CODE ] tags:

Code:
[B]Object   Reference[/B] 
String   s          10010101010
 
Yes, or put another more useful way - shallow copies can be overwritten by modifying the source object, deep copies are not changed by modifying the original source object.
Shallow copies allow you to shoot yourself in the foot more easily, in other words.
 

Similar threads

Replies
1
Views
1K
  • · Replies 10 ·
Replies
10
Views
6K
  • · Replies 7 ·
Replies
7
Views
12K
Replies
89
Views
7K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 28 ·
Replies
28
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
3
Views
4K
  • · Replies 17 ·
Replies
17
Views
5K