Differences between JAVA depth and shortest cloning

Source: Internet
Author: User

You cannot simply use the clone method to clone objects in JAVA. The clone method is just a shortest clone. See the following:
 
Deep clone class:
Java code
Import java. io. ByteArrayInputStream;
Import java. io. ByteArrayOutputStream;
Import java. io. IOException;
Import java. io. ObjectInputStream;
Import java. io. ObjectOutputStream;
 
Public class DepthClone {

Public final static Object objectCopy (Object oldObj ){
Object newObj = null;

Try {
ByteArrayOutputStream bo = new ByteArrayOutputStream ();
ObjectOutputStream oo = new ObjectOutputStream (bo );
Oo. writeObject (oldObj); // source object
ByteArrayInputStream bi = new ByteArrayInputStream (bo. toByteArray ());
ObjectInputStream oi = new ObjectInputStream (bi );
NewObj = oi. readObject (); // target object
} Catch (IOException e ){
E. printStackTrace ();
} Catch (ClassNotFoundException e ){
E. printStackTrace ();
}

Return newObj;
}
}
 
Shortest clone class:
Java code
Import java. io. Serializable;
 
Public class SimpleClone implements Cloneable, Serializable {
 
Private static final long serialVersionUID =-7552721152118950502L;

Public String s = null;
Public Object o = null;
Public CloneObject cloneObject = null;


Public SimpleClone (CloneObject cloneObject ){
This. cloneObject = cloneObject;
}
 
Public Object clone (){
SimpleClone newSimpleClone = null;
Try {
/* Shortest clone */
NewSimpleClone = (SimpleClone) super. clone ();
} Catch (CloneNotSupportedException e ){
E. printStackTrace ();
}
Return newSimpleClone;
}
}
 
Clone object:
Java code
Import java. io. Serializable;
 
Public class CloneObject implements Serializable {

Private static final long serialVersionUID = 4332788486669750696L;
 
Private String name;
 
Public String getName (){
Return name;
}
 
Public void setName (String name ){
This. name = name;
}

}
 
Clone test:
Java code
Public class TestClone {

Public static void main (String [] arg ){
CloneObject obj1 = new CloneObject ();
Obj1.setName ("cloneOne ");
CloneObject obj2 = new CloneObject ();
Obj2.setName ("cloneTwo ");

SimpleClone simpleClone1 = new SimpleClone (obj1 );
SimpleClone simpleClone2 = new SimpleClone (obj2 );

SimpleClone1.s = "simpleClone1 ";
SimpleClone1.o = "simpleClone1Object ";

// SimpleClone2 = (SimpleClone) simpleClone1.clone ();

SimpleClone2 = (SimpleClone) DepthClone. objectCopy (simpleClone1 );

/* If it is a shortest clone, the cloneObject in simpleClone1 will change the surface with the cloneObject in simpleClone2 */
SimpleClone2.cloneObject. setName ("cloneThree ");

System. out. println (simpleClone1.cloneObject. getName ());
System. out. println (simpleClone2.cloneObject. getName ());
}

}

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.