What are the differences between shallow and deep copies?

  • Thread starter Thread starter Chromium
  • Start date Start date
AI Thread Summary
The discussion centers on the concepts of shallow and deep copies in programming, particularly in Java. A shallow copy creates a new reference to the same object, meaning multiple references point to the same memory location. This can lead to unintended modifications if one reference changes the object. In contrast, a deep copy creates a completely new object with its own memory allocation, ensuring that changes to the new object do not affect the original. The conversation also touches on the clarity of the provided examples and the importance of understanding these concepts to avoid potential pitfalls in programming. Overall, the distinction between shallow and deep copies is emphasized, highlighting the implications of each on object manipulation.
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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...
Back
Top