Java two methods to achieve deep copy of the object
1, if a class does not implement the Cloneable interface direct call clone () method, will be reported abnormal CloneNotSupportedException, I already wrote in Object source code:
-
* @return a clone of this instance.
-
* @exception CloneNotSupportedException if the object's class does not
-
* support the {@code Cloneable} interface. Subclasses
-
* that override the {@code clone} method can also
-
* throw this exception to indicate that an instance cannot
-
* be cloned.
-
* @see java.lang.Cloneable
-
*/
-
protected native Object clone() throws CloneNotSupportedException;
Moreover, the source code is also written to the clone Object () method is a shallow copy, which is in Object source code analysis before I have written a.
2, a custom class that implements the deep copy method has two kinds, specific wording given below sequentially.
2.1, custom class to implement the Cloneable interface and override clone () method.
-
/**
-
* Deep copy and shallow copy of testing
-
*/
-
// Test category 1
-
class Person implements Cloneable{
-
String name;
-
int age;
-
Person(String name, int age){
-
this.name=name;
-
this.age=age;
-
}
-
-
public Object clone() {
-
try{
-
return super.clone();
-
} catch(CloneNotSupportedException e){
-
return null;
-
}
-
}
-
}
-
// test class 2
-
-
class Animal implements Cloneable{
-
Host the Person; // owner
-
Age int; // old
-
Animal(Person person, int age){
-
this.host=person;
-
this.age=age;
-
}
-
-
public Object clone(){
-
try{
-
Animal animal=(Animal) super.clone();
-
= animal.host (the Person) host.clone (); // process a deep copy
-
return animal;
-
} catch (CloneNotSupportedException e){
-
return null;
-
}
-
}
-
}
-
-
//test
-
public class Main{
-
public static void main(String[] args) {
-
Person person1= new Person("cxh",26);
-
Person person2=(Person)person1.clone();
-
System.out.println ( "shallow copy ---------------- --------------");
-
// Object clone method for testing shallow copy
-
// String class == test is consistent with the memory address
-
System.out.println ( "PERSON1 person2 and the same memory address name:" + (person1.name == person2.name) );
-
-
-
-
System.out.println ( "---------------- -------------- deep copy");
-
// override the clone method of Object, achieve deep copy
-
// or use == to see two objects are equal memory address to determine whether the two objects, and if two memory addresses, then that is a deep copy
-
Animal animal1= new Animal(new Person("cxh",26),3);
-
Animal animal2=(Animal) animal1.clone();
-
System.out.println ( "animal1 animal2 the host and the same memory address:" + (animal1.host == animal2.host) );
-
}
-
}
Output:
-
---------------- -------------- shallow copy
-
person1 and person2 the name memory address is the same: to true
-
---------------- -------------- deep copy
-
animal1 and animal2 the host memory address is the same: false
-
-
Process finished with exit code 0
A very detailed explanation of the blog: http://blog.csdn.net/zhangjg_blog/article/details/18369201
2.2, is achieved by way of a deep copy of the sequence: first to copy the object is written to the byte stream in the memory, and then reads the byte stream from the information stored just as a new object is returned, then the new object and share original object does not exist on any address, to achieve a natural deep copy.
Custom class needs to implement Serializable interface.
-
import java.io. *;
-
-
/**
-
* Deep copy and shallow copy of testing
-
* How to use serialized copy of the object to complete it? Copies of the byte stream is relatively easy to implement in memory through. The parent object is written to a byte stream, then it will read from the byte stream,
-
* This will create a new object, and there is no reference and sharing issues between the new object and parent objects, a deep copy of the real object.
-
*/
-
//Tools
-
class CloneUtil{
-
public static <T extends Serializable> T clone(T obj){
-
T cloneObj= null;
-
try{
-
// write byte stream
-
ByteArrayOutputStream baos= new ByteArrayOutputStream();
-
ObjectOutputStream oos= new ObjectOutputStream(baos);
-
oos.writeObject (obj);
-
oos.close ();
-
-
// allocate memory, write the original object, generate a new object
-
Bais = A ByteArrayInputStream new new A ByteArrayInputStream (baos.toByteArray ()); // Get the above output byte stream
-
ObjectInputStream ois= new ObjectInputStream(bais);
-
-
// returns a new object generated
-
cloneObj=(T)ois.readObject();
-
ois.close();
-
} catch (Exception e){
-
e.printStackTrace ();
-
}
-
return cloneObj;
-
}
-
}
-
-
// Test category 1
-
class Person implements Serializable{
-
String name;
-
int age;
-
Person(String name, int age){
-
this.name=name;
-
this.age=age;
-
}
-
-
}
-
// test class 2
-
-
class Animal implements Serializable{
-
Host the Person; // owner
-
Age int; // old
-
Animal(Person person, int age){
-
this.host=person;
-
this.age=age;
-
}
-
}
-
-
-
//test
-
public class Main{
-
public static void main(String[] args) {
-
System.out.println ( "---------------- -------------- deep copy");
-
// override the clone method of Object, achieve deep copy
-
// or use == to see two objects are equal memory address to determine whether the two objects, and if two memory addresses, then that is a deep copy
-
Animal animal1= new Animal(new Person("cxh",26),3);
-
Animal animal2=CloneUtil.clone(animal1);
-
System.out.println ( "animal1 animal2 the host and the same memory address:" + (animal1.host == animal2.host) );
-
}
-
}
Output:
-
---------------- -------------- deep copy
-
animal1 and animal2 the host memory address is the same: false
Reference blog: http://blog.csdn.net/chenssy/article/details/12952063