“This is the second day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”
Related articles
Java with Notes: With notes
preface
-
There are three ways to copy a reference from one object to another.
- Direct assignment
- Shallow copy
- Deep copy
- All three concepts are really about copying objects
-
So what’s the difference between these three copies?
1. Direct assignment
- In Java, a1 = a2, we need to understand that this is actually copying A reference, meaning a1 and A2 refer to the same object. Therefore, when A1 changes, the member variables in A2 also change.
- I don’t think there’s much to say about this. No case studies.
Shallow copy
-
Create a new object and copy the non-static fields of the current object into the new object, or if the fields are of value type. If the field is a reference type, the reference is copied but not the referenced object. Therefore, the original object and its copy refer to the same object.
-
class Resume implements Cloneable { public Object clone() { try { return (Resume) super.clone(); } catch (Exception e) { e.printStackTrace(); return null; }}}Copy the code
-
You can implement the Cloneable interface yourself. Click on super.clone(); What the hell is that?
-
It used to be the native method.
- Native methods are, as you all know, not written in Java, but called by Java.
- Because Java programs run on the JVM, there is no way to access the lower-level, operating system-specific information, but only in languages close to the operating system.
-
Because each class’s direct or indirect parent is Object, they all have a clone() method, but because it is protected, none of them can be accessed outside the class.
-
To make a copy of an object, you need to override the clone method.
Deep copy
-
The deep copy not only copies the object itself, but also all objects that the object contains references to.
-
What if we have properties in one object that are in another object?
-
@Data public class teacher { private String name; private int age; private Student student; } Copy the code
-
@Data class Student implements Cloneable { private String name; private int age; } Copy the code
-
-
How do you replicate in this case? In fact, by rewriting clone() method can also achieve deep cloning, but more troublesome. No introduction here.
-
Through object serialization and deserialization to achieve cloning, you can achieve true deep cloning.
- Serialization is the process of writing an object to a stream, which is a copy of the original object that still exists in memory.
- Copy through serialization can copy not only the object itself, but also the member objects it refers to.
- Thus, by serializing an object to a stream and then reading it out of the stream, a deep clone can be implemented.
-
@Data public class Teacher implements Serializable { private String name; private int age; private Student student; public Teacher(String name, int age, Student student) { this.name = name; this.age = age; this.student = student; } // deepClone public Object deepClone() throws IOException, ClassNotFoundException {// serialize ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(this); Bis = new ByteArrayInputStream(bos.tobytearray ()); ObjectInputStream ois = new ObjectInputStream(bis); return ois.readObject(); }}Copy the code
-
@Data @AllArgsConstructor public class Student implements Serializable { private String name; private int age; } Copy the code
-
Public class test {public static void main(String[] args) {try {Student s = new Student(" big fish ", 24); Teacher origin = new Teacher(" ding Teacher ", 32, s); System.out.println(" clone student name: "+ origine.getstudent ().getname ()); Teacher clone = (Teacher) origin.deepClone(); Clone. GetStudent (). SetName (" I am the clone of big Fish 2"); Println (" clone student name: "+ clone.getstudent ().getname ()); }catch (Exception e){ e.printStackTrace(); }}}Copy the code
-
Execution Result:
The road ahead is long, I see no end, I will search high and low
If you think I bloggers write well! Writing is not easy, please like, follow, comment and give encouragement to the blogger ~ Hahah