Recently read some books, deep copy and shallow copy of the words, always appear in front of the eyes, and then organized a bit, about the principle and implementation of the depth of Java.
1. What is a deep/shallow copy?
Shallow copy: Copy of the base data type value of the target object and the address of the reference type;
Deep copy: A copy of the base data type value of the target object and the application of the Type value copy (the professional point is called dynamic memory).
Conceptually, it is difficult to understand the difference between the two:
1) Shallow copy
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/58/D1/wKiom1S9t4OhLGxEAAD9wJ6hH2Y403.jpg "title=" 2.png " alt= "Wkiom1s9t4ohlgxeaad9wj6hh2y403.jpg"/>
, the left side is the original object (the base data type, the reference type), but a new object is generated with a shallow copy so that the base type value of the new object is consistent with the original object, but the reference data type points to the original object reference type, so that when either the original object or the shallow copy object modifies the value of the reference type, Then the other side will change too. Because both point to the same object.
2) deep copy
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/58/D1/wKiom1S9uIyx_MaXAAEVbYJBDDc784.jpg "title=" 2.png " alt= "Wkiom1s9uiyx_maxaaevbyjbddc784.jpg"/>
There is no reference to the connection relationship between the original object and the deep copy object. A deep copy actually executes a new object that generates a new object, the base data type and the original object, and the reference data type is constructed from the original object reference data type value. The popular point is that the reference data of the deep copy object is the same as the reference data value of the original object, but not the same object and does not point to the same address.
2. Java implementation Deep/copy
public class student {private string name;private int age;public Student (string name, int age) {this.name = name;this.age = age;} Public string getname () {return name;} Public void setname (String name) {this.name = name;} Public int getage () {return age;} Public void setage (Int age) {this.age = age;}} public class classes implements cloneable{private string classname;private Student student;public classes (string classname, student student) {This.className = classname;this.student = student;} /** * default implementation is a shallow copy of */@Overridepublic object clone () throws Clonenotsupportedexception {return super.clone ();} /** * deep Copy : Create a new object * @return */public classes deepcopy () &nbSp {classes classes = new classes (This.getclassname (), new student (This.getStudent ( ). GetName (), this.getstudent (). Getage ()); return classes;} Public string getclassname () {return classname;} Public void setclassname (String classname) {this.classname = classname;} Public student getstudent () {return student;} Public void setstudent (student student) {this.student = student;}} Test Code public class testcopy {@Testpublic void testshallowcopy () throws clonenotsupportedexception{classes classes = new classes ("091", new Student ("Wangzp", 23));// shallow copy, create a new object, then copy the base data type value, reference type address classes classes1 = (Classes) Classes.clone (); System.out.println (Classes.getstudent () == classes1.getstudent ())//true} @Testpublic void Testdeepcopy () {classes classes = new classes ("091", new student ("Wangzp", 23)); Classes classes1 = classes.deepcopy (); System.out.println (Classes1.getstudent () == classes.getstudent ());//false}}
3. Differences in deep/shallow copies
From the 2 code implementation, you can find that deep copy is the process of re-structuring the object, so the efficiency is not very good. and shallow copy because the reference object is shared between the copied object and the original object, it is easy to cause either party to modify the reference type property, and the other party modifies it, which can lead to the error.
In general, a shallow copy is recommended if the original object variable is mostly of the base data type, and the reference type is not often modified, whereas a deep copy is recommended.
This article is from the "Java Program Ghost" blog, please be sure to keep this source http://793404905.blog.51cto.com/6179428/1606011
Deep copy and shallow copy of Java