preface
Deep copy: a separate memory space is created for the data members of pointer type before the assignment, so that the true contents are copied. This copy is called a deep copy.
Two implementations of deep copy
- Cloneable interface, rewrite the Object class clone() method, the implementation of layer clone method.
- By Serializable, objects are written to and then read from a stream. Although this method is very inefficient, but this method is the true sense of deep cloning.
Serialization and deserialization
Serialization: The process of converting an object into a sequence of bytes is called object serialization. Deserialization: The process of restoring a sequence of bytes to an object is called deserialization of an object.
The instance
-
Start by constructing a student object. It is worth noting that the class of the serialized object must also implement the Serializable interface, otherwise NotSerializableException will be thrown.
import java.io.Serializable; / * * *@author CodeLuo * @date2021/7/7 "* / public class Student implements Serializable{ private String name; private String address; private String sex; public String getName(a) { return name; } public void setName(String name) { this.name = name; } public String getAddress(a) { return address; } public void setAddress(String address) { this.address = address; } public String getSex(a) { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString(a) { return "Student{" + "name='" + name + '\' ' + ", address='" + address + '\' ' + ", sex='" + sex + '\' ' + '} '; }}Copy the code
-
Create the StudentClone. Java class that implements deep copy
import java.io.*; / * * *@author CodeLuo * @date2021/7/7 "* / public class StudentClone implements Serializable { public static Student myClone(Student student){ Student anotherStu = null; try { // Create a buffer in memory to serialize objects into streams ByteArrayOutputStream bout = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bout); out.writeObject(student); // Find this buffer and deserialize the byte stream into another object ByteArrayInputStream bais = new ByteArrayInputStream(bout.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bais); anotherStu = (Student) ois.readObject(); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } returnanotherStu; }}Copy the code
test
The test method is to create a student object and an empty object, and deeply copy the student properties into anotherStudnet by calling the copy method.
public static void main(String[] args) { Student student = new Student(); student.setName("Zhang"); student.setAddress("The earth"); student.setSex("Male"); System.out.println("First student:"+student); Student anotherStudent = null; System.out.println("Second student before Deep Copy:"+anotherStudent); anotherStudent = myClone(student); System.out.println("Second student after Deep Copy:"+anotherStudent); } Copy the code
The test results
After the program runs, the test results are shown in the figure below, and the copy is successfully completed.
Pay attention to
- If a serialized object does not implement the Serializable interface in its class, the NotSerializableException exception seen in the appeal occurs. As shown in the figure below.
-
Say you don’t want a property of an object to be serialized in terms of business requirements. Such as involving sensitive ID numbers or mobile phone numbers. Of course sensitive information can also be encrypted, haha. In this case, you can add the TRANSIENT keyword before the attribute.
private String name; private String address; // Suppose you don't want the gender to be copied this time. private transient String sex; Copy the code