1. Shallow copy and deep copy concept
(1) shallow copy (shallow clone)
A shallow copy is also called shallow copy, and all the fields in the object are copied to the new object (copy). Where the value of the Value Type field (the original type in Java 8) is copied to the replica, the modifications in the copy do not affect the value corresponding to the source object. If a reference type's field is copied to a reference of a reference type, rather than a referenced object, modifying the field value of the reference type in the copy affects the source object itself.
Shallow copy simple generalization is to copy only one object, the inside of the object points to other objects, the array or reference is not copied.
(2) deep copy (Deep clone)
Copies all the fields in the object to the new object. However, whether the object's value Type field, or reference Type field, is recreated and assigned a value, modifications to the copy do not affect the source object itself.
A deep copy is a simple generalization of objects that are referenced inside the object are copied.
2. The Clone () method in Java
(1) The Clone () method copies a copy of the object and returns it to the caller. Generally, the clone () method satisfies the following specification:
① to any object x, there are X.clone ()! = x;//The cloned object is not the same object as the original object
② has X.clone () for any object X. getclass () = = X.getclass ();//Clone the object as the type of the original object
③ if the Equals () method of object x is defined appropriately, then X.clone (). equals (x);
(2) Cloning of objects in Java
The Clone () method is defined in object and is protected, only the class that implements the Cloneable interface can invoke the Clone () method on its instance, otherwise it throws clonenotsupportexception.
In order to get a copy of the object, we can use the Clone () method of the Objects class, or implement the Cloneable interface, overriding the Clone () method of the base class, and calling Super.clone () in the Clone () method.
The Cloneable interface is a labeled interface, that is, without any content, as defined below:
Package Java.lang;
Pubilc Interface cloneable{
}
The example code is as follows:
Class Student implements Cloneable { String name; int age; Student (String Name,int age) { this.name=name; this.age=age; } Public Object Clone () { object o=null; Clone () in try{//object identifies which object you want to copy o= (Student) Super.clone (); } catch (Clonenotsupportedexception e) { System.out.println (e.tostring ()); } return o; } } public static void Main (string[] args) { Student s1=new Student ("Zhangsan"); Student s2= (Student) S1.clone (); S2.name= "Lisi"; s2.age=20; System.out.println ("Name=" +s1.name+ "," + "age=" +s1.age);//Modify Student 2, does not affect the value of 1 students. }
Description
① Why do we have to call Super.clone () when we override the Clone () method of object in a derived class?
At run time, Clone () in object identifies which object you want to copy, then allocates space for the object, replicates the object, and copies the contents of the original object one by one into the storage space of the new object.
The
② inherits from the Clone () method of the Java.lang.Object class is shallow copy. The following code can prove that:
The professor does not implement the Cloneable interface, and the Clone () method of the Java.lang.Object class is used by default class professor{String name; int age; Professor (String Name,int age) {this.name=name; This.age=age; }}//student implements the Cloneable interface class Student implements cloneable{String name;//constant object. int age; Professor P; Student (String name,int age,professor p) {this.name=name; This.age=age; This.p=p; } public Object Clone () {Student o=null; try{o= (Student) Super.clone (); }catch (clonenotsupportedexception e) {System.out.println (e.tostring ()); }//Use the Clone () method of the object class o.p= (professor) This.p.clone (); return o; }} public static void Main (string[] args) {Professor P=new professor ("Wangwu", 50); Student s1=new Student ("Zhangsan", 18,p); Student s2= (Student) S1.clone (); S2.p.name= "Lisi"; s2.p.age=30; 1 of the students ' professors also became lisi,age for 30. System.out.println ("Name=" +s1.p.name+ "," + "age=" +s1.p.age); }
How should a deep-seated clone be implemented, that the professor who modifies S2 will not affect S1 's professors? The code is improved as follows:
The Professor class implements the Cloneable interface and no longer uses the object default Clone () method class Professor implements cloneable{String name; int age; Professor (String Name,int age) {this.name=name; This.age=age; } public Object Clone () {object o=null; try{O=super.clone (); }catch (clonenotsupportedexception e) {System.out.println (e.tostring ()); } return o; }} class Student implements cloneable{String name; int age; Professor P; Student (String name,int age,professor p) {this.name=name; This.age=age; This.p=p; } public Object Clone () {Student o=null; try{o= (Student) Super.clone (); }catch (clonenotsupportedexception e) {System.out.println (e.tostring ()); }//Call the Clone () method of the Professor class to implement the deep copy o.p= (professor) This.p.clone (); return o; }} public static void Main (string[]args) {Professor P=new professor ("Wangwu", 50); Student s1=new Student ("Zhangsan", 18,p); Student s2= (Student) S1.clone (); S2.p.name= "Lisi"; s2.p.age=30; Students 1 of professors do not change System.out.println ("Name=" +s1.p.name+ "," + "age=" +s1.p.age); }
3. Using serialization to make deep copies
The process of writing an object into a stream is a serialization (serilization) process, which also calls the object to serialize, while the (deserialization) process of reading the object from the stream is called deserialization. It should be noted that in the stream is a copy of the object, and the original object is still in the JVM, so deep in the Java language to copy an object, it is often possible to implement the object serializable interface, and then the object (actually just a copy of the object) written into a stream, Then you can reconstruct the object by reading it from the stream.
The following is a deep copy of the source code:
public Object Deepclone () {//writes the object into the stream Bytearrayoutoutstream bo=new bytearrayoutputstream (); ObjectOutputStream oo=new ObjectOutputStream (bo); Oo.writeobject (this); Read it out of the stream Bytearrayinputstream bi=new Bytearrayinputstream (Bo.tobytearray ()); ObjectInputStream oi=new ObjectInputStream (BI); Return (Oi.readobject ()); This is done only if the object and all objects referenced inside the object are serializable, otherwise it is necessary to carefully examine whether the serializable objects can be set to transient, thereby excluding them from the replication process. The above example code is improved as follows. Class Professor implements serializable{String name; int age; Professor (String Name,int age) {this.name=name; This.age=age; }} class Student implements serializable{String name; int age; Professor P; Student (String name,int age,professor p) {this.name=name; This.age=age; This.p=p; } public Object Deepclone () throws ioexception,optionaldataexception,classnotfoundexception{//writes the object into the stream Byte Arrayoutputstream bo=new Bytearrayoutputstream (); ObjEctoutputstream oo=new ObjectOutputStream (bo); Oo.writeobject (this);//object of studnet//read out from the stream bytearrayinputstream bi=new Bytearrayinputstream (Bo.tobytearra Y ()); ObjectInputStream oi=new ObjectInputStream (BI); Return (Oi.readobject ()); } public static void Main (string[] args) {Professor P=new professor ("Wangwu", 50); Student s1=new Student ("Zhangsan", 18,p); Student s2= (Student) S1.deepclone (); S2.p.name= "Lisi"; s2.p.age=30; Students 1 of professors do not change System.out.println ("Name=" +s1.p.name+ "," + "age=" +s1.p.age); } }
Deep and shallow copies in Java