Detailed description of java object deep cloning based on serialized access

Source: Internet
Author: User

We know that in java, after a non-prototype object is referenced and assigned to another object, the two references point to the same object, for example: Copy codeThe Code is as follows: public class DeepCloneTest {

Private class CloneTest {
Private Long myLong = new Long (1 );
}

Public static void main (String args []) {
New DeepCloneTest (). Test ();
}

Public void Test (){
CloneTest ct1 = new CloneTest ();
CloneTest ct2 = ct1;

// To see if ct1 and ct2 are one same reference.
System. out. println ("ct1:" + ct1 );
System. out. println ("ct2:" + ct2 );

// If ct1 and ct2 point to one same object, then ct1.myLong = ct2.myLong.
System. out. println ("ct1.myLong:" + ct1.myLong );
System. out. println ("ct2.myLong:" + ct2.myLong );

// We change ct2's myLong
Ct2.myLong = 2L;

// To see whether ct1's myLong was changed.
System. out. println ("ct1.myLong:" + ct1.myLong );
System. out. println ("ct2.myLong:" + ct2.myLong );
}
}

Out put:
Ct1: DeepCloneTest $ CloneTest @ c17164
Ct2: DeepCloneTest $ CloneTest @ c17164
Ct1.myLong: 1
Ct2.myLong: 1
Ct1.myLong: 2
Ct2.myLong: 2
This is easy. I guess all I know about java (I don't know what I know about java ?).
In the memory, object references are stored in the stack, object data is stored in the heap, and stack references point to objects in the heap. The references in the two stacks point to the same object in the heap. Therefore, when myLong of ct2 is changed, the myLong value of ct1 is also changed, if it is represented by a graph, it is easy to understand:

There are two references in the stack area on the left, with the same value. They point to the same object in the stack area on the right.
Most of the time, we will use this feature of the java language to do what we want to do, for example, passing the object reference as an input parameter into a method, in the method, modify the referenced object accordingly. However, sometimes we want to construct an object with the same content as an existing object, but reference different objects. Therefore,You can do this.:Copy codeThe Code is as follows: public class DeepCloneTest {

// Must implements Cloneable.
Private class CloneTest implements Cloneable {
Private Object o = new Object ();

Public CloneTest clone (){
CloneTest ct = null;
Try {
Ct = (CloneTest) super. clone ();
} Catch (CloneNotSupportedException e ){
E. printStackTrace ();
}
Return ct;
}
}

Public static void main (String args []) {
New DeepCloneTest (). Test ();
}

Public void Test (){
CloneTest ct1 = new CloneTest ();
CloneTest ct2 = ct1.clone ();

// To see if ct1 and ct2 are one same reference.
System. out. println ("ct1:" + ct1 );
System. out. println ("ct2:" + ct2 );

// Whether ct1.o = ct2.o? Yes
System. out. println ("ct1.o" + ct1.o );
System. out. println ("ct1.o" + ct1.o );
}
}

Out put:
Ct1: DeepCloneTest $ CloneTest @ c17164
Ct2: DeepCloneTest $ CloneTest @ 1fb8ee3
Ct1.o java. lang. Object @ 61de33
Ct1.o java. lang. Object @ 61de33
From the output, we can see that ct1 and ct2 are indeed two different references, so we take it for granted that ct1.o and ct2.o are two different objects, but we can see from the output that this is not the case! Ct1.o and ct2.o are the same object! The reason is that although cloning is used, the above is just a simple clone, represented in graphics:


Have you seen the above o? Actually, two objects are shared. This is equivalent to the fact that you had a sheepfold 1 with a sheep in it, and then you made another sheepfold 2 without pulling the sheep out of the sheepfold 1, the sheep are also circled in the sheepfold 2, you think you have two sheep, actually? Everyone knows.

This is the result of the shortest clone:If you want two objects to have independent o, you must clone o. Some people may think that there is nothing to do, but they have never thought about it. If there is more than one o, there are many o-like things. Do you clone them one by one? Obviously, it is unrealistic.

One solution is:The object is serialized and stored in the stream first, and then read from the reserved object. This ensures that the read data is identical to the previous object, and the values in it are identical, it is like a complete copy.Copy codeThe Code is as follows: import java. io. ByteArrayInputStream;
Import java. io. ByteArrayOutputStream;
Import java. io. IOException;
Import java. io. ObjectInputStream;
Import java. io. ObjectOutputStream;
Import java. io. Serializable;
Public class DeepCloneTest {

// Must implements Cloneable.
Private class CloneTest implements Serializable {
Private static final long serialVersionUID = 1L;
Private Object o = new Object ();

Public CloneTest deepClone (){
CloneTest ct = null;
Try {
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
ObjectOutputStream oos = new ObjectOutputStream (baos );
Oos. writeObject (this );
ByteArrayInputStream bais = new ByteArrayInputStream (baos. toByteArray ());
ObjectInputStream ois = new ObjectInputStream (bais );
Ct = (CloneTest) ois. readObject ();
} Catch (IOException e ){
E. printStackTrace ();
} Catch (ClassNotFoundException e ){
E. printStackTrace ();
}
Return ct;
}
}

Public static void main (String args []) {
New DeepCloneTest (). Test ();
}

Public void Test (){
CloneTest ct1 = new CloneTest ();
CloneTest ct2 = ct1.deepClone ();

// To see if ct1 and ct2 are one same reference.
System. out. println ("ct1:" + ct1 );
System. out. println ("ct2:" + ct2 );

// Whether ct1.o = ct2.o? No
System. out. println ("ct1.o" + ct1.o );
System. out. println ("ct1.o" + ct1.o );
}
}

At this time, the data in the memory is like this:

The cloning task is completed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.