Article directory
foreword
First of all, the so-called deep copy and shallow copy are for reference types. Before talking about deep copy and shallow copy, you need to have a brief understanding of the Java memory model, and know the allocation principles of different data in memory, so that you can better understand what deep copy and shallow copy mean.
The following is an excerpt from the introduction of "In-depth Understanding of Java Virtual Machine: JVM Advanced Features and Best Practices (3rd Edition)":
definition
With the above understanding, we can know that for the reference type, only the reference of the object is stored in the stack, and the specific data points to the object instance data in the heap, just like the bank card in your hand is just a reference, It corresponds to the bank account, on which is your deposit, and a series of operations can be performed on the account through the bank card.
Shallow copy: Only the variable references in the stack are copied, and the two references point to the same object instance data in the heap.
It is equivalent to a shallow copy that just copies a secondary card. The secondary card and the primary card correspond to the same account. If one card pays a sum of money, the balance of the other card must change synchronously.
Deep copy: Create a new object instance in the heap, copy the source object instance data to the new object instance, and the copied object points to the new object instance data, which are independent of each other and do not affect each other.
It is equivalent to deep copy not only copying a card, but also opening a new account synchronously. The same deposit (currency and amount) as the main card is deposited in the account. The expenditure and income of one card have no effect on the other card at all. .
The schematic diagram is as follows:
shallow copy verification
String type/basic type/wrapper type
1. After the value of the source object changes, it has no effect on the copy object
2. Assert that the source object and the copy object are not the same reference
reference type
- Assert that the A a property of the two objects is the same reference object, the test passes
- It can also be seen from the results printed on the console that the modification of the original object affects the copy object
deep copy implementation
No matter what method is used, as long as the copied new object is guaranteed, the instance data in the heap is independent.
Overloading the clone method - not recommended!
Among the many recognized specifications, it is more cautious to rewrite the clone method, and this article does not provide an example of this method. If it is really necessary, it is easy to find the relevant demo code on the Internet. Or there are a lot of demos in "Effective Java" that demonstrate how to correctly use the clone method
to list two related code specifications:
1. According to the "Java Development Manual (Huangshan Edition)":
25.【推荐】慎用 Object 的 clone 方法来拷贝对象。
说明:对象 clone 方法默认是浅拷贝,若想实现深拷贝需覆写 clone 方法实现域对象的深度遍历式拷贝。
2. "Effective Java" also clearly pointed out that the clone method should be overridden carefully
Constructor
For the reference type attribute (field) of the copied object, create a new reference object through new, copy the value of the source object to the new object, and ensure that the copied object and the source object have independent object instances. 深拷贝示意图``deepCopyRef()
As in the method in the legend above
Deserialization - Advice
deep copy tools
MapStruct
It is very simple to use, you only need to write a class, and declare the mapping relationship through annotations. The official provides very rich usage examples.
Official open source address: https://github.com/mapstruct/mapstruct
Official demo: https://github.com/mapstruct/mapstruct-examples
sample code
https://gitee.com/qbhj/java-cases/tree/master/case-javase-deepcopy
References
- "In-depth understanding of Java virtual machine: JVM advanced features and best practices (3rd edition)" - Zhou Zhiming
- "Effective Java Chinese Edition 2nd Edition" - Joshua Bloch
- "Java Development Manual (Huangshan Edition)"
- MapStruct