Shallow copy:
Packagetest;classStudentImplementscloneable {Private intNumber ; Public intGetNumber () {returnNumber ; } Public voidSetnumber (intNumber ) { This. Number =Number ; } @Override PublicObject Clone () {Student stu=NULL; Try{stu= (Student)Super. Clone (); } Catch(clonenotsupportedexception e) {e.printstacktrace (); } returnStu; }} Public classTest { Public Static voidMain (String args[]) {Student stu1=NewStudent (); Stu1.setnumber (12345); Student STU2=(Student) Stu1.clone (); System.out.println ("Student 1:" +Stu1.getnumber ()); System.out.println ("Student 2:" +Stu2.getnumber ()); Stu2.setnumber (54321); System.out.println ("Student 1:" +Stu1.getnumber ()); System.out.println ("Student 2:" +Stu2.getnumber ()); }}
Deep copy:
Packagetest2;classAddressImplementscloneable{PrivateString add; PublicString Getadd () {returnadd; } Public voidSetadd (String add) { This. Add =add; } @Override PublicObject Clone () {Address stu=NULL; Try{stu= (Address)Super. Clone (); } Catch(clonenotsupportedexception e) {e.printstacktrace (); } returnStu; }}classStudentImplementscloneable {Private intNumber ; PrivateAddress addr; PublicAddress getaddr () {returnaddr; } Public voidsetaddr (Address addr) { This. addr =addr; } Public intGetNumber () {returnNumber ; } Public voidSetnumber (intNumber ) { This. Number =Number ; } @Override PublicObject Clone () {Student stu=NULL; Try{stu= (Student)Super. Clone (); } Catch(clonenotsupportedexception e) {e.printstacktrace (); } stu.addr=(Address) Addr.clone (); returnStu; }} Public classTest { Public Static voidMain (String args[]) {Address addr=NewAddress (); Addr.setadd ("Hangzhou City"); Student STU1=NewStudent (); Stu1.setnumber (123); Stu1.setaddr (addr); Student STU2=(Student) Stu1.clone (); System.out.println ("Student 1:" + stu1.getnumber () + ", Address:" +stu1.getaddr (). Getadd ()); System.out.println ("Student 2:" + stu2.getnumber () + ", Address:" +stu2.getaddr (). Getadd ()); Addr.setadd ("West L. District"); System.out.println ("Student 1:" + stu1.getnumber () + ", Address:" +stu1.getaddr (). Getadd ()); System.out.println ("Student 2:" + stu2.getnumber () + ", Address:" +stu2.getaddr (). Getadd ()); }}
Using serialization for deep replication, the process of writing an object into a stream is a serialization (serilization) process, and the process of reading an object from the stream is called a deserialization (deserialization) process. It should be noted that a copy of the object is written in the stream, and the original object still exists inside the JVM. , you can make a deep copy with this feature.
ImportJava.io.ByteArrayInputStream; ImportJava.io.ByteArrayOutputStream; ImportJava.io.ObjectInputStream; ImportJava.io.ObjectOutputStream; Importjava.io.Serializable; //using serialization to make deep copies//Deep Clone Public classDeepclonetest { Public Static voidMain (string[] args)throwsException {//teacher objects are shared by student objects that are not clone-out. Teacher Teacher =NewTeacher (); Teacher.setage (40); Teacher.setname ("Teacher Zhang"); Student Student1=NewStudent (); Student1.setage (20); Student1.setname ("Zhangsan"); Student1.setteacher (teacher); //copy out an object Student2Student Student2 =(Student) student1.deepcopy (); System.out.println (Student2.getage ()); System.out.println (Student2.getname ()); System.out.println ("~~~~~~~~~~~~~~~~~~~~~~"); System.out.println (Student1.getteacher (). Getage ()); System.out.println (Student1.getteacher (). GetName ()); //Modifying a Student2 Reference objectStudent2.getteacher (). Setage (50); Student2.getteacher (). SetName ("Teacher Li"); System.out.println ("~~~~~~~~~~~~~~~~~~~~~~"); System.out.println (Student1.getteacher (). Getage ()); System.out.println (Student1.getteacher (). GetName ()); } } classTeacherImplementsSerializable {Private Static Final LongSerialversionuid = -8834559347461591191l; Public intAge ; PublicString name; Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } } classStudentImplementsSerializable {//Serialversionuid//if your object is serialized and then saved to the hard disk, but later you change the field of the class (increase or decrease or rename), when you deserialize, there will be exception, which will cause incompatibility problems. //But when the serialversionuid is the same, it assigns a different field to the default value of type (such as the int type is the 0,string type is NULL, etc.), which avoids the problem of incompatibility. So it's best to assign Serialversionuid Private Static Final LongSerialversionuid = 7991552226614088458L; Public intAge ; PublicString name; PublicTeacher Teacher; Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicTeacher Getteacher () {returnteacher; } Public voidSetteacher (Teacher Teacher) { This. Teacher =teacher; } PublicObject deepcopy ()throwsException {//serializes the object into a stream because it is written in a stream that is a copy of the object, and the original object still exists inside the JVM. So the deep copy of the object can be realized by using this feature.Bytearrayoutputstream BOS =NewBytearrayoutputstream (); ObjectOutputStream Oos=NewObjectOutputStream (BOS); Oos.writeobject ( This); //serializing a stream into an objectBytearrayinputstream bis =NewBytearrayinputstream (Bos.tobytearray ()); ObjectInputStream Ois=NewObjectInputStream (bis); returnOis.readobject (); The result of the output is:20Zhangsan~~~~~~~~~~~~~~~~~~~~~~ 40Teacher Zhang~~~~~~~~~~~~~~~~~~~~~~ 40Teacher Zhang
Shallow copy and deep copy in Java