Home » Java » A shallow copy of Java clone and deep copy

A shallow copy of Java clone and deep copy

The cloning method in

java is clone ();


when using the clone () method to copy an object, the new object after copying is two different objects from the old object,


content:


old object.Clone () = new object


old object (.GeClass) (

) = new object

old object (object) = true


but, if the object members of other variables contain references to objects when using clone (Reference) with the old object references to the same object is a new method of object clone, change the properties of any object references in the impact on the other one. This is clone (the shallow copy

method)

so, how will the shallow copy into a deep copy of the implementation of the Cloneable interface, to achieve their own clone method, clone method, and in reference to the object is a clone device, a new object that cloned to other object references as a copy, the deep copy of


public, Object, clone () {
Student o = null;
{try
O = (Student) super.clone ();
O.p = (Person) (p.clone); / / reference is also a clone
} catch (CloneNotSupportedException, e) {
E.printStackTrace ();
}
Return o;
}


Similarly, deep copy

can be serialized using serialization

process stream is written object serialization (Serilization) process, but in the Java programmer circles very vividly called "frozen" or "pickled vegetables (picking)" process; while the parallel object read from the stream of (Deserialization) process is called "thaw" or "fresh (depicking)".
It should be noted that , written in the stream is a copy of the original object, and the object still exists in JVM, so "pickled into pickles" is just a copy of the object, Java can also return.
fresh pickles
deep copy an object in the Java language, can often be the object that implements the Serializable interface, then the object (actually just a copy of the object) to a stream (pickled into pickles), and then read out from the stream (the pickle back fresh) can reconstruct the object.
.


public, Object, deepClone () {
ByteArrayOutputStream bos;
ObjectOutputStream oos;
ByteArrayInputStream bis;
ObjectInputStream ois;
Object o =null;
Try {// objects into the stream
Bos=, new, ByteArrayOutputStream ();
OOS = new, ObjectOutputStream (Bos);
Oos.writeObject (this);
/ / object stream readout
BIS = new, ByteArrayInputStream (bos.toByteArray ());
OIS = new, ObjectInputStream (BIS);
O=, ois.readObject ();
Catch (IOException ClassNotFoundException e |} {)
E.printStackTrace ();
}
Return o;
The premise is the application of such}// and variables in the object implementing the Serializable interface


Test:


public, static, void, main (String[], args) {
Person P = new Person ("Wang Xiaoer", "22", "Beijing");
Student S1 = new Student ("Xiao Ming", 12, P);
Student = S2 (Student) (s1.deepClone); / / (Student) (s1.clone);
System.out.println ("S1 object" +s1);
System.out.println ("S2 object" +s2);
System.out.println ("S1 object age": "+s1.age+", "S1 object name", "+s1.name+", "S1 object reference object" Person: "," +s1.p ");
System.out.println ("S2 object age": "+s2.age+", "S2 object name", "+s2.name+", "S2 object reference object" Person: "," +s2.p ");
}
test results


s1 object com.isoftstone.Student@d3db51

S2 object com.isoftstone.Student@bad8a8

S1 object age: 12 S1 object name: Xiaoming S1 object reference object Person:com.isoftstone.Person@888e6c

S2 object age: 12 S2 object name: Xiaoming S2 object reference object Person:com.isoftstone.Person@e61fd1




if you reference a member variable object with transient, and then call the deepClone () method, referencing the object will not copy the


test results


s1 object com.isoftstone.Student@17494c8

S2 object com.isoftstone.Student@e020c9

S1 object age: 12 S1 object name: Xiaoming S1 object reference object Person:com.isoftstone.Person@888e6c

S2 object age: 12 S2 object name: Xiaoming S2 object reference object Person:null


transient explains: (by:, Baidu, Wikipedia)


Java serialization provides a mechanism for persistent object instances. When the persistent object, there may be a special object data members, we don't want to use the serialization mechanism to save it. In order to close the serialization in a domain of a particular object, in this domain can be added before keywords transient. when an object is serialized, transient values are not included in the serialized representation, but non transient variables are included in the.



copyright notice: This article is original article for blogger. Without permission from blogger,.

must not be reprinted

Latest