zzh

zzh

Java Deep Copy and Shallow Copy

Assuming we have a class.

We use two images below to illustrate the difference between shallow copy and deep copy.

  • Shallow Copy

Untitled Form

  • Deep Copy
    Untitled Form (2)
Notes:#

Assuming we have a class and we perform a shallow copy.
image
At this point, the output result is
image
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.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.