The copy of the object is then cloned into the object. There are two types of objects: Shallow copy and deep copy.
Shallow copy
Shallow copy (shallow clone) all the variables of the copied object contain the same value as the original object, and all references to other objects still point only to the original object, in other words, shallow copy only the object that the lock is considered for, not the object it refers to.
The shallow copy of the object is implemented by invoking the Clone method.
A shallow copy requires attention to detail:
1. If an object needs to invoke clone's method clone, then the class to which the object belongs must implement the Cloneable interface.
2. The cloneable interface is just an identity interface, and there is no method.
3. Shallow clones of objects are also not called to the constructor method.
code example:
1 //entity Class Code2 classaddress{3 4 String City;5 6 PublicAddress (String city) {7 This. City =City ;8 }9 Ten } One A Public classPersonImplementscloneable{ - intID; - the String name; - - address address; - + PublicPerson (intID, String name) { - This. ID =ID; + This. Name =name; A } at PublicPerson (intID, String name, address address) { - This. ID =ID; - This. Name =name; - This. Address =address; -System.out.println ("======= constructor method called = = ="); - } in @Override - PublicString toString () { to return"No.:" + This. id+ "Name:" + This. name+ "Address:" +address.city; + } - @Override the PublicObject Clone ()throwsclonenotsupportedexception { * return Super. Clone (); $ }Panax Notoginseng } - //Test Class Code the Public classdemocopy { + Public Static voidMain (string[] args)throwsclonenotsupportedexception { Aperson P1 =NewPerson ("Nick",NewAddress ("Jinan")); theperson P2 =(person) p1.clone (); +p2.address.city = "Beijing"; -System.out.println ("P1 content:" +p1); $System.out.println ("P2 content:" +p2); $ } -}View Code
Deep copy
Deep copy (deep clone) all the variables of the copied object contain the same values as the original object, removing the variables that refer to other objects, and those that refer to other objects will point to the new objects that have been copied, and no longer try the original referenced objects, in other words, Deep copy copies the objects that are referenced by the object being copied again.
The process of writing objects into a stream is a serialization (serilization) process, but is also very visually called "Frozen" or "pickled pickles (picking)" In the Java programming community. The parallelization (deserialization) process, which reads objects from the stream, is called a "thaw" or "depicking" process. It should be noted that the write in the stream is a copy of the object, and the original object is still in the JVM, so "pickle" is only a copy of the object, Java Pickles can also be returned fresh.
In the Java language, a deep copy of an object can often be used to implement the Serializable interface, and then the object (actually a copy of the object) is written into a stream (pickled pickles), and then read out from the stream (pickles back fresh), you can reconstruct the object.
The deep copy of the object is implemented through the object input (objectinputstream) output (ObjectOutputStream) stream.
The sample code is as follows:
1 //entity Class Code2 classAddressImplementsserializable{3 4 String City;5 6 PublicAddress (String city) {7 This. City =City ;8 }9 Ten } One A Public classPersonImplementsserializable{ - intID; - the String name; - - address address; - + PublicPerson (intID, String name) { - This. ID =ID; + This. Name =name; A } at PublicPerson (intID, String name, address address) { - This. ID =ID; - This. Name =name; - This. Address =address; -System.out.println ("======= constructor method called = = ="); - } in @Override - PublicString toString () { to return"No.:" + This. id+ "Name:" + This. name+ "Address:" +address.city; + } - the } * //Test Code $ Public classDemo2 {Panax Notoginseng - Public Static voidMain (string[] args)throwsIOException, ClassNotFoundException { theAddress address =NewAddress ("Guangzhou"); +person P1 =NewPerson (110, "Dog Doll"), address); A Writeobj (p1); theperson P2 =readobj (); + -p2.address.city = "Changsha"; $System.out.println ("P1:" +p1); $System.out.println ("P2:" +p2); - - the } - Wuyi the //and then read the object's information from the file. - Public StaticPerson Readobj ()throwsClassNotFoundException, ioexception{ WuFileInputStream FileInputStream =NewFileInputStream ("F:\\obj.txt"); - //Create an input stream object for an object AboutObjectInputStream ObjectInputStream =NewObjectInputStream (fileinputstream); $ return(person) objectinputstream.readobject (); - } - - A //write the object to the file first. + Public Static voidWriteobj (Person P)throwsioexception{ the //Create an output stream object for a file -FileOutputStream FileOutputStream =NewFileOutputStream ("F:\\obj.txt"); $ //Create an output stream for an object theObjectOutputStream ObjectOutputStream =NewObjectOutputStream (fileoutputstream); the //write the object out the Objectoutputstream.writeobject (p); the //Close Resource - objectoutputstream.close (); in the } the}View Code
Copy of Object