Deep replication and light Replication

Source: Internet
Author: User

(1) shallow copy (shallow clone)
All the variables of the Copied object contain the same value as the original object, and all
Still point to the original object. In other words, the shortest replication only copies the object to be considered, instead of copying the object
The referenced object.

(2) Deep replication (deep cloning)
All variables of the Copied object contain the same value as the original object.
Variable. Variables that reference other objects will point to new objects that have been copied, instead of the original ones.
The referenced object. In other words, deep replication copies all the objects referenced by the objects to be copied.

2. Java clone () method
(1) The clone method copies an object and returns it to the caller. Generally, the clone () method meets the following requirements:
① Any object X has X. Clone ()! = X // The cloned object is not the same as the original object
② For any object X, X. Clone (). getclass () = x. getclass () // The cloned object is of the same type as the original object.
③ If the equals () method of object X is properly defined, X. Clone (). Equals (x) should be established.

(2) cloning of objects in Java
① To obtain a copy of an object, we can use the clone () method of the object class.
② Override the clone () method of the base class in the derived class and declare it as public.
③ Call Super. Clone () in the clone () method of the derived class ().
④ Implement the cloneable interface in the derived class.

-----------------------------------------------------------

Shallow copy: Only one object is copied. If an object exists inside the object and points to another object array or reference, the object is not copied.
Deep copy: copying objects and internal references of Objects
In order to better understand their differences, we assume there is an object A, which contains two objects, A1 and A2.

After object A is copied in a shortest, object B is obtained, but object A1 and A2 are not copied.

Object A performs a deep copy to obtain that both A1 and A2 are copied along with their references.

After understanding Deep copy and shallow copy, let's take a look at Java's deep copy and shallow copy implementations.
By default, the clone () method of Java. Lang. Object returns a shortest copy object. Therefore, to implement a deep copy using the clone () method, we must implement the clone () method of each object. When the object hierarchy is complex, it is not only difficult, but also time-consuming and prone to errors. In particular, when you not only need to copy the object in depth, but also make a small copy of the object, you will find that writing this clone () method is really not a good solution.

In addition to the clone () method, you can also use serialization to perform copy operations. Output the object to be copied
Byte array, and then use objectinputstream to convert the new object

Public class test implements serializable {

Private string name = "hello ";

protected object clone (Object OBJ) {
Object object = NULL;
try {
bytearrayoutputstream out = new bytearrayoutputstream ();
objectoutputstream outputstream = new objectoutputstream (out);
outputstream. writeobject (OBJ);
outputstream. flush ();
outputstream. close ();

Bytearrayinputstream in = new bytearrayinputstream (Out. tobytearray ());
Objectinputstream inputstream = new objectinputstream (in );

Object = inputstream. readobject ();
} Catch (ioexception e ){
E. printstacktrace ();
} Catch (classnotfoundexception e ){
E. printstacktrace ();
}
Return object;
}

Public static void main (string ARGs []) {
test = new test ();
test one = (TEST) test. clone (TEST);
one. name = "world";
system. out. print (test. name + "" + one. name);
}< BR >}

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.