Assuming we have a class.
We use two images below to illustrate the difference between shallow copy and deep copy.
- Shallow Copy
- Deep Copy
Notes:#
Assuming we have a class and we perform a shallow copy.
At this point, the output result is
In other words, the shallow copy is not performed as shown in the previous illustration because the values in object x and object y are not the same. This is because String and Integer, as well as other wrapper classes, are immutable objects. When modifying the value of an immutable object, a new object needs to be created in memory to store the new value, and then the original reference is pointed to the new address.