There are two main ways to clone Java objects: Shallow cloning and deep cloning
First, don't confuse the object's cloning with the assignment of the object, see
P2 = p1; an assignment operation that simply assigns the assigned object to the address of the previous object, actually the physical memory is a piece, and the result of the cloning operation should be two objects pointing to the same two blocks of memory respectively. This is the state after the clone operation:
Here are shallow clones and deep clones:
The difference between deep and shallow clones occurs when there are external class objects in the class, such as the object in the person class that has the address class
1 Packagecn.itcast.copy;2 3 Importjava.io.Serializable;4 5 classAddressImplementsserializable{6 7 String City;8 9 PublicAddress (String city) {Ten This. City =City ; One } A } - - the Public classPersonImplementscloneable,serializable { - - intID; - + String name; - + address address; A at PublicPerson (intID, String name) { - This. ID =ID; - This. Name =name; - } - - in PublicPerson (intID, String name, address address) { - This. ID =ID; to This. Name =name; + This. Address =address; -System.out.println ("======= constructor method called = = ="); the } * $ Panax Notoginseng @Override - PublicString toString () { the return"No.:" + This. id+ "Name:" + This. name+ "Address:" +address.city; + } A the + @Override - PublicObject Clone ()throwsclonenotsupportedexception { $ return Super. Clone (); $ } -}
Then, if the object of the person class will involve the problem of shallow cloning and deep cloning, the first two graphs show the difference between shallow clone and deep clone.
The first figure is a shallow clone result, the second is a deep clone result, which means that a shallow clone can clone only the current object, cannot clone the object of the outer class to which the current object is pointing, and a deep clone is a clone of all associated data and objects.
Let's say two kinds of cloning implementations:
If a class may involve cloning operations, so long need to implement the Cloneable interface, deep cloning also need to implement the Serializable interface (shallow clone not), such as the person on the top of the implementation of the relevant interface, shallow cloning only need to invoke the implementation of the Clone method can be:
1 Public classDemo1 {2 3 4 Public Static voidMain (string[] args)throwsException {5Address address =NewAddress ("Guangzhou");6person P1 =NewPerson (110, "Dog Doll"), address);7person P2 = (person) p1.clone ();//Clone () clones an object. 8 9P2.name = "Dog Left";Tenp2.address.city = "Changsha"; OneSystem.out.println ("P1:" +p1); ASystem.out.println ("P2:" +p2); - - } the -}
Shallow cloning principle is very simple, its simplicity and replication as good understanding, deep cloning different, in order to achieve deep cloning, the need to be cloned object to the development file, and then the contents of the file to the new object to complete the cloning, so it involves the flow operation:
1 Public classDemo2 {2 3 Public Static voidMain (string[] args)throwsIOException, ClassNotFoundException {4Address address =NewAddress ("Guangzhou");5person P1 =NewPerson (110, "Dog Doll"), address);6 Writeobj (p1);7person P2 =readobj ();8 9p2.address.city = "Changsha";TenSystem.out.println ("P1:" +p1); OneSystem.out.println ("P2:" +p2); A } - - //and then read the object's information from the file. the Public StaticPerson Readobj ()throwsClassNotFoundException, ioexception{ -FileInputStream FileInputStream =NewFileInputStream ("F:\\obj.txt"); - //Create an input stream object for an object -ObjectInputStream ObjectInputStream =NewObjectInputStream (fileinputstream); + return(person) objectinputstream.readobject (); - } + A //write the object to the file first. at Public Static voidWriteobj (Person P)throwsioexception{ - //Create an output stream object for a file -FileOutputStream FileOutputStream =NewFileOutputStream ("F:\\obj.txt"); - //Create an output stream for an object -ObjectOutputStream ObjectOutputStream =NewObjectOutputStream (fileoutputstream); - //write the object out in Objectoutputstream.writeobject (p); - //Close Resource to objectoutputstream.close (); + - } the *}
Cloning of Java objects