Deep and shallow cloning in Java
Why clone
First, consider the question, why do you need to clone objects? Can't you just new an object?
The cloned object may contain some modified properties, and the properties of the new object are still the values at the time of initialization, so it is necessary to clone when a new object is needed to save the "state" of the current object
Of course, it is also possible to assign the attributes of the object to the new new object one by one, but in this way, there is no trouble. Second, We can see from the source code that the clone method of an object is a native method (the native method is a code implemented in a non Java language for Java programs to call. There is no way to access the underlying operating system related code, which can only be implemented by a language close to the operating system). That's fast. It's actually implemented at a lower level
Our common object a = new object(); Object b; b = a; This form of code copies the reference, that is, the address of the object in memory. A and B point to the same object The object assigned by clone method and the original object exist independently at the same time
concept
Shallow clone:
All variable values in the cloned object are the same as the original object, and all references to other objects still point to the original object In short, shallow cloning only clones the current object, not the object referenced by the current object
Deep cloning:
All variables in the cloned object have the same values as the original object. Variables that reference other objects will point to the copied new object instead of the original referenced object In short, deep cloning not only clones the current object, but also copies all the objects referenced by the current object
Clone in object
The clone () method in the object class is a shallow clone Its working principle is as follows: first open up the same space as the original object in memory, and then copy the contents of the original object For basic data types, this is no problem, but for reference types, because only object references are saved, the cloned past references point to the same object
Shallow cloning in Java
To implement clone in Java, you need to implement the clonable interface, which is very simple. The source code is as follows:
It only serves as an identification
The following is an example of shallow cloning:
It can be seen that the object is not the original object, but the reference object is still the original object
Shallow cloning copies references only for reference objects
If an object contains only original data or immutable object fields (such as string), shallow cloning is recommended
Deep cloning in Java
Clone all reference types in the class and override the object clone () method to clone all reference types
The code is as follows:
Clone all reference types to achieve deep cloning
Java serialization clone
If reference types include reference types, it will be troublesome to implement multi-layer cloning, which can use serialization and deserialization to realize deep cloning of objects
The process of writing objects to the byte stream is a serialization process, while the process of reading objects from the byte stream is a deserialization process In the process of Java serialization, what is written in the stream is a copy of the object, and the original object is still in the JVM, so this principle can be used to realize the deep cloning of the object
The above code uses serialization to achieve the following:
The serialized clone can be encapsulated as a method, as follows:
This tool class enables deep cloning If this method is used, the object, all reference types of the object, and the reference types in the reference types must implement the serializable interface
Of course, the final object in the object cannot be cloned because of the limitation of final
If you use a thread safe class to implement clonable, you should ensure that its clone method does a good job of synchronization. The default object The clone method is not synchronized