- #1
Chromium
- 56
- 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!)
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: