Java allows us to clone an object, often in decorative and prototype mode. So what is deep cloning and what is shallow cloning?
"Shallow clone", usually just copy the cloned instance, but the other child objects inside are common.
"Deep clone", when cloning will copy its sub-object reference, inside all the variables and sub-objects are additional copies of a copy.
The following two examples can be very good to illustrate their differences:
First look at the class diagram
The husband class has a reference to wife, and when a shallow clone is made, the wife variable points to the same wife, while a deep clone points to a different wife. Verify the following:
"Shallow Clone"
1 PublicObject Clone () {2Husband Husband =NULL;3 Try{4Husband =(Husband) Super.clone ();5}Catch(clonenotsupportedexception e) {6 e.printstacktrace ();7}finally{8 returnhusband;9 }Ten}
"Deep Clone"
1 PublicObject Deepclone () throws Ioexception,classnotfoundexception {2 //write an object into the stream3Bytearrayoutputstream BOS =NewBytearrayoutputstream ();4ObjectOutputStream Oos =NewObjectOutputStream (BOS);5Oos.writeobject ( This);6 //read it back from the stream.7Bytearrayinputstream bis =NewBytearrayinputstream (Bos.tobytearray ());8ObjectInputStream Ois =NewObjectInputStream (bis);9 returnOis.readobject ();Ten}
"All Code"
1 Package Com.xingoo.clone;2 3 import Java.io.ByteArrayInputStream;4 import Java.io.ByteArrayOutputStream;5 import java.io.IOException;6 import Java.io.ObjectInputStream;7 import Java.io.ObjectOutputStream;8 import java.io.Serializable;9 import java.util.Date;Ten One classWife implements serializable{ A PrivateString name; - PrivateDate birthday; - the PublicWife () { -Name ="Sister Furong"; -Birthday =NewDate (); - } + PublicDate Getbirthday () { - returnbirthday; + } A at PublicString GetName () { - returnname; - } - Public voidsetName (String name) { - This. Name =name; - } in } - classHusband implements cloneable,serializable{ to PrivateWife Wife; + PrivateDate birthday; - the PublicHusband () { *wife =NewWife (); $Birthday =NewDate ();Panax Notoginseng } - the PublicWife Getwife () { + returnwife; A } the + PublicDate Getbirthday () { - returnbirthday; $ } $ /** - * Shallow clone an object - */ the PublicObject Clone () { -Husband Husband =NULL;Wuyi Try{ theHusband =(Husband) Super.clone (); -}Catch(clonenotsupportedexception e) { Wu e.printstacktrace (); -}finally{ About returnhusband; $ } - } - /** - * Use serialization to deeply clone an object, read the object and its references to the stream, and write the other objects A * @return + * @throws IOException the * @throws classnotfoundexception - */ $ PublicObject Deepclone () throws Ioexception,classnotfoundexception { the //write an object into the stream theBytearrayoutputstream BOS =NewBytearrayoutputstream (); theObjectOutputStream Oos =NewObjectOutputStream (BOS); theOos.writeobject ( This); - //read it back from the stream. inBytearrayinputstream bis =NewBytearrayinputstream (Bos.tobytearray ()); theObjectInputStream Ois =NewObjectInputStream (bis); the returnOis.readobject (); About } the } the Public classTest { the Public Static voidMain (string[] args) { + Try{ -Husband Husband =NewHusband (); theSystem. out. println ("husband Birthday"+husband.getbirthday (). GetTime ());BayiSystem. out. println ("Wife Birthday"+Husband.getwife (). Getbirthday (). GetTime ()); theSystem. out. println (); theHusband Husband1 =(Husband) Husband.clone (); -System. out. println ("Husband1 Birthday"+husband1.getbirthday (). GetTime ()); -System. out. println ("Wife Birthday"+Husband1.getwife (). Getbirthday (). GetTime ()); theSystem. out. println (); theSystem. out. println ("whether it is the same husband"+ (husband = =husband1)); theSystem. out. println ("whether it is the same wife"+ (husband.getwife () = =Husband1.getwife ())); theSystem. out. println (); -Husband Husband2 =(Husband) Husband.deepclone (); theSystem. out. println ("Husband2 Birthday"+husband2.getbirthday (). GetTime ()); theSystem. out. println ("Wife Birthday"+Husband2.getwife (). Getbirthday (). GetTime ()); theSystem. out. println ();94System. out. println ("whether it is the same husband"+ (husband = =husband2)); theSystem. out. println ("whether it is the same wife"+ (husband.getwife () = =Husband2.getwife ())); the}Catch(Exception e) { the e.printstacktrace ();98 } About } -}
"Run Results"
1414247244668141424724466814142472446681414247244668 false True 1414247244668 1414247244668falsefalse
Java Development Series--deep clones and shallow clones