The assignment of objects in Java is divided into shallow and deep copies.
1. Object Shallow Copy
1 Public classclonetest{2 Static classemp{3 String name;4 intAge ;5 Date hiredate; 6 }7 Public Static voidMain (string[] args) {8EMP emp1=NewEmp ();9EMP emp2=emp1;Ten } One}
In the case of a "=" assignment, this is a shallow copy of the object. In memory, both EMP1 and EMP2 point to the same object in the Java heap (if the virtual machine lets the reference store the object's address directly, it points to the same memory address, and if the reference stores a handle, it points to the same handle).
In this case, whether we modify the EMP1 or modify the EMP2 will appear in another reference. But under normal circumstances we want an object that is the same as the original object, but not the same one. This is where we have to use object cloning.
2. Object Deep Copy
2.1 Object Cloning
The premise that an object can be cloned is that it must inherit the Cloneable interface. The interface does not contain anything but a token, which means that all objects of the class can be cloned. Then use the public modifier to redefine the Clone method, because the Clone method is defined as protected in the object class, which is also a shallow copy by default.
protected native throws Clonenotsupportedexception;
When you overwrite the Clone method, you can use the covariant technique to return a specific type to the Clone class.
1 Public classEmpImplementscloneable{2 PrivateString name;3 Private intAge ;4 PrivateDate hiredate; 5 6 @Override7 protectedEMP Clone ()throwsclonenotsupportedexception {8EMP emp = (EMP)Super. Clone ();9Emp.hiredate=(Date) Hiredate.clone ();Ten returnEMP; One } A}
Here we see that the date class can call the Clone method without error because the date class inherits the Cloneable interface.
Public class implements java.io.Serializable, cloneable, comparable<date>
But what do we do when we use a component in our class that does not inherit the Cloneable interface? Another problem is that using the Clone method means that all objects on the chain of objects associated with the object inherit the Cloneable interface. This is a bit too much trouble, and a class in the design of the time do not know if he will be cloned, when you need to clone the time to notify the class programmer to add cloneable inheritance to the class? This is obviously unreasonable. Therefore, this use of the Clone method to implement a deep copy of the object is not recommended .
2.2 Object serialization for deep copy
As long as the corresponding class is serializable. It's simple: Serialize the object directly into the output stream and read it back. The resulting new object is a deep copy of the existing object. In this process, we do not have to write the object out to a file, because you can save the data in a byte array with Bytearrayoutputstream.
1 Public classSerialcloneableImplementscloneable,serializable {2 Private Static Final LongSerialversionuid = 7403553044775279221L;3 PublicObject Clone () {4 Try {5 //save the object to a byte array6Bytearrayoutputstream bout=NewBytearrayoutputstream ();7ObjectOutputStream out=NewObjectOutputStream (bout);8Out.writeobject ( This);9 out.close ();Ten //Read Clone of the object from the byte array OneBytearrayinputstream bin=NewBytearrayinputstream (Bout.tobytearray ()); AObjectInputStream in=NewObjectInputStream (bin); -Object ret=In.readobject (); - in.close (); the returnret; -}Catch(Exception e) { - return NULL; - } + } -}
In a simpler way, to get clone, you just need to extend the Serialcloneable class so you're done. However, it should be used with caution, as it is usually much slower than the cloning method that displays the object and replicates or clones the data domain.
Replication of Java Objects