Java Deep clone and light clone 1

Source: Internet
Author: User

Copying is getting a copy

Cloning is copying a copy of an object. However, there may be basic data Types in an object, such as: Int,long,float, etc., and also contain non-basic data types such as (arrays, collections, etc.)
The value of the base type of the object being cloned is modified, and the value of the original object does not change. This is suitable for shadow Clone (Shallow clone).


But if you want to change the value of a non-basic type , the value of the original object changes. For example, an array, only copy his address in memory, and this address point to the value is not copy, when clone, two addresses point to a value , So once this value has changed, the original value has changed, too, because they share a value. It must be deep clone

For data of reference types, cloning is simply a reference address in the stack for copying non-basic type data from the source object, causing the copied object to be the same as the reference address of the source object, which can modify the content of non-basic data types in the source object, affecting the copied object

1. Shallow copy and deep copy concept
⑴ Shallow copy (shallow clone)
All the variables of the copied object contain the same value as the original object, and all references to other objects still point to the original object. In other words, a shallow copy simply duplicates the object being considered, without

Copy the object that it refers to.

⑵ deep Copy (Deep clone)
All the variables of the copied object contain the same values as the original object, removing the variables that refer to other objects. Variables that refer to other objects will point to new objects that have been copied, not the original

Some of those objects that are referenced. In other words, a deep copy copies the objects that are referenced by the object being copied again.

2. The Clone () method of Java
The ⑴clone method copies a copy of the object and returns it to the caller. Generally, the clone () method satisfies:
① 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 properly, then X.clone (). Equals (x) should be established.

Cloning of objects in ⑵java
① in order to get a copy of the object, we can take advantage of the Clone () method of the Objects class.
② overrides the Clone () method of the base class in the derived class and declares it to be public.
③ in the Clone () method of the derived class, call Super.clone ().
④ implements the Cloneable interface in a derived class.

Take a look at the following code:

First,
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;
Try
{
o= (Student) Super.clone (); Clone () in//object identifies which object you want to copy.
}
catch (Clonenotsupportedexception e)
{
System.out.println (E.tostring ());
}
return o;
}
}

public static void Main (string[] args)
{
Student s1=new Student ("Zhangsan", 18);
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, allocates space for the object, and replicates the object.

Copies the contents of the original object one by one into the storage space of the new object. ()
② inherits from the Clone () method of the Java.lang.Object class is a shallow copy. The following code can prove it.


Class Professor
{
String name;
int age;
Professor (String Name,int age)
{
This.name=name;
This.age=age;
}
}
Class Student implements Cloneable
{
String name;//constant object.
int age;
Professor p;//the reference value for student 1 and Student 2 is the same.
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 ());
}
O.p= (professor) 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;
System.out.println ("Name=" +s1.p.name+ "," + "age=" +s1.p.age);//students 1 of professors become lisi,age for 30.
}


How should the deep-seated cloning be achieved, that the professor who modifies S2 will not affect S1 's professors? The code is improved as follows.

Improvement makes the professor of 1 students unchanged (deep cloning)
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 ());
}
O.p= (professor) 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;
System.out.println ("Name=" +s1.p.name+ "," + "age=" +s1.p.age);//students 1 of professors do not change.
}
3. Using serialization to make deep copies
The process of writing objects into a stream is a serialization (serilization) process, but is also very visually called "Frozen" or "pickled pickles (picking)" In the Java programming community. The parallelization (deserialization) process, which reads objects from the stream, is called a "thaw" or "depicking" process.

It should be noted that the write in the stream is a copy of the object, and the original object is still present in the JVM, so "pickle" is only a copy of the object, Java Pickles can also be fresh.
In the Java language deep copy of an object, often can be implemented serializable interface, and then the object (actually just a copy of the object) written into a stream (pickled pickles), and then read out from the stream (pickles back fresh), you can reconstruct the object. The
below 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 out from 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 non-serializable objects can be set to transient, thereby excluding them from the replication process. The above example code is improved as follows.
Class Teacher implements serializable{
String name;
int age;
Teacher (String name,int age) {
This.name=name;
This.age=age;
}
}
Class Student implements serializable{
String name;//constant Object
int age;
Teacher t;//The reference value for student 1 and Student 2 is the same.
Student (String name,int age,teacher t) {
This.name=name;
This.age=age;
This.p=p;
}
Public Object Deepclone () throws IOException,
optionaldataexception,classnotfoundexception{//writes the object to the stream
Bytearrayoutoutstream bo=new bytearrayoutputstream ();
ObjectOutputStream oo=new ObjectOutputStream (bo);
Oo.writeobject (this);//read out from the stream
Bytearrayinputstream bi=new bytearrayinputstream (Bo.tobytearray ());
ObjectInputStream oi=new ObjectInputStream (BI);
Return (Oi.readobject ());
}

}
public static void Main (string[] args) {
Teacher t=new Teacher ("Tangliang", 30);
Student s1=new Student ("Zhangsan", 18,t);
Student s2= (Student) S1.deepclone ();
S2.t.name= "Tony";
s2.t.age=40;
System.out.println ("Name=" +s1.t.name+ "," + "age=" +s1.t.age);//Student 1 teacher does not change
}

Java Deep clone and light clone 1

Related Article

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.