Deep copy in Java (deep copy) and shallow copy (shallow copy) Introduction _java

Source: Internet
Author: User
Tags constant serialization shallow copy

Deep copy (deep copy) and shallow copy (shallow copy) are two more common concepts, especially in the C + + language, if not understood, it will be in the delete when the problem, but we are fortunate to use Java. Although Java automatically manages the collection of objects, we should pay enough attention to deep copy (deep copy) and shallow copy (shallow copy), because sometimes these two concepts often bring us no small confusion.

A shallow copy is a copy of an object that simply copies the object itself (including the basic variables in the object), not the object that the object contains. A deep copy not only copies the object itself, but also copies all objects that the reference to the object contains. For example, it is clearer that the object A1 contains references to B1, B1 contains references to C1. The shallow copy A1 gets a2,a2 still contains references to B1, and B1 still contains references to C1. Deep copy is recursive for shallow copy, deep copy A1 get a2,a2 contains references to B2 (B1 copy), B2 contains references to C2 (C1 copy).

If you do not overwrite the Clone () method, the object that you call this method is a shallow copy, and we will focus on the deep copy below.

Run the following program and take a look at the shallow copy:

Class Professor0 implements cloneable {String name; 
 
  int age; 
    Professor0 (String name, int age) {this.name = name; 
  This.age = age; 
  Public Object Clone () throws Clonenotsupportedexception {return super.clone (); 
  } class Student0 implements cloneable {String name;//constant object. 
  int age; 
 
  The reference values for PROFESSOR0 p;//1 and Student 2 are the same. 
    Student0 (String name, int age, Professor0 p) {this.name = name; 
    This.age = age; 
  THIS.P = p; 
    Public Object Clone () {Student0 o = null; 
    try {o = (Student0) super.clone (); 
    catch (Clonenotsupportedexception e) {System.out.println (e.tostring ()); 
  return o; } public class Shallowcopy {public static void main (string[] args) {Professor0 p = new Professor0 ("Wangwu" 
    , 50); 
    Student0 S1 = new Student0 ("Zhangsan", p); 
    Student0 s2 = (Student0) s1.clone (); 
    S2.p.name = "Lisi"; 
    S2.p.age = 30; 
    S2.name = "Z"; s2.aGE = 45; System.out.println ("Student S1 's name:" + s1.name + "\ n student S1 's name:" + S1.p.name + "," + "student S1 professor's age" + s1.p.age);//Student 1 professor}}

The

S2 has changed, but the S1 has also changed, proving that S1 p and S2 p are pointing to the same object. This is not the case in our actual needs, so we need a deep copy:

Class Professor implements cloneable {String name; 
 
  int age; 
    Professor (String name, int age) {this.name = name; 
  This.age = age; 
    public Object Clone () {object o = null; 
    try {o = Super.clone (); 
    catch (Clonenotsupportedexception e) {System.out.println (e.tostring ()); 
  return o; 
  Class Student implements cloneable {String name; 
  int age; 
 
  Professor P; 
    Student (String name, int age, Professor P) {this.name = name; 
    This.age = age; 
  THIS.P = p; 
    Public Object Clone () {Student o = null; 
    try {o = (Student) super.clone (); 
    catch (Clonenotsupportedexception e) {System.out.println (e.tostring ()); 
    } O.P = (professor) P.clone (); 
  return o; 
    } public class Deepcopy {public static void main (String args[]) {Long T1 = System.currenttimemillis (); 
    Professor P = new Professor ("Wangwu", 50); Student S1 = new StudeNT ("Zhangsan", p); 
    Student s2 = (Student) s1.clone (); 
    S2.p.name = "Lisi"; 
    S2.p.age = 30; 
    System.out.println ("name=" + S1.p.name + "," + "age=" + s1.p.age);//Student 1 professor does not change. 
    Long t2 = System.currenttimemillis (); 
  System.out.println (T2-T1); } 
}

Of course, we also have a deep copy method, which is to serialize the object:

Import java.io.*; Serialization is time-consuming class Professor2 implements Serializable {/** * */private static final L 
  Ong serialversionuid = 1L; 
  String name; 
 
  int age; 
    Professor2 (String name, int age) {this.name = name; 
  This.age = age; 
  Class Student2 implements Serializable {/** * */private static final long serialversionuid = 1L; 
  String name;//constant object. 
  int age; 
 
  The reference values for Professor2 p;//1 and Student 2 are the same. 
    Student2 (String name, int age, Professor2 p) {this.name = name; 
    This.age = age; 
  THIS.P = p; public Object Deepclone () throws IOException, Optionaldataexception, classnotfoundexception {//write object to stream 
    Li Bytearrayoutputstream bo = new Bytearrayoutputstream (); 
    ObjectOutputStream oo = new ObjectOutputStream (bo); 
    Oo.writeobject (this); 
    read out of the stream bytearrayinputstream bi = new Bytearrayinputstream (Bo.tobytearray ()); ObjectInputStream oi = new Objectinputstream (BI); 
  Return (Oi.readobject ()); } public class DeepCopy2 {/** * @param args/public static void main (string[] args) throws Optio 
    Naldataexception, IOException, classnotfoundexception {long T1 = System.currenttimemillis (); 
    Professor2 p = new Professor2 ("Wangwu", 50); 
    Student2 S1 = new Student2 ("Zhangsan", p); 
    Student2 s2 = (Student2) s1.deepclone (); 
    S2.p.name = "Lisi"; 
    S2.p.age = 30; System.out.println ("name=" + S1.p.name + "," + "age=" + s1.p.age); 
    Students 1 of the professors do not change. 
    Long t2 = System.currenttimemillis (); 
  System.out.println (T2-T1); } 
 
}

But serialization is time-consuming, and in some frameworks we can feel that they tend to serialize objects and pass them for more time-consuming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.