Java in the "shallow copy" and "deep copy"

copy

  The reference to an object is copied to another object, a total of three ways. The first way is direct assignment, the second way is shallow copy, the third way is a deep copy.

1. Direct assignment

  In Java, A a1 = a2, which is actually copies the reference, that is a1 and a2 point to the same object. Therefore, when the change a1, a2 which will follow the member variables change.

2. shallow copy (copy references, but does not copy the object referenced)

 Shallow copy, also known as shallow copy. Create a new object, then copy the current object non-static fields to the new object, if the field is a value type, then executed copy of the field; if the reference type to this field, then copy the reference to the object but does not copy references. Therefore, a copy of the original object and refer to the same object.

 1 class Resume implements Cloneable{ 
 2      public Object clone() { 
 3          try { 
 4              return (Resume)super.clone(); 
 5          } catch (Exception e) { 
 6              e.printStackTrace(); 
 7          return null; 
 8          } 
 9      } 
10 } 

3. deep copy (copy the object and its reference to the object)

  Also known as a deep copy, copy only the object itself, and copy all objects contained references to the objects.

 1 class Student implements Cloneable {
 2          String name;
 3          int age;
 4          Professor p;
 5      Student(String name, int age, Professor p) {
 6          this.name = name;
 7          this.age = age;
 8          this.p = p;
 9      }
10      public Object clone() {
11          Student o = null;
12          try {
13              o = (Student) super.clone();
14          } catch (CloneNotSupportedException e) {
15              System.out.println(e.toString());
16          }
17      o.p = (Professor) p.clone();
18      return o;
19      }
20 }

The method of cloning steps use a target clone ():

  1) was cloned class to implement Cloneable interface.

  2) was cloned to override class clone () method.

  So in programming, how to select which copy method to use it? First, check the class has nothing basic types (i.e., object) data members. If not, super.clone return () can be. If so, member variables to ensure all non-basic types of classes are included to achieve a deep copy.

Extended: the difference between deep and shallow copy copy?

  Shallow copy (Shallow Clone): All variables are copied object contains the same value as the original target, and all other references to objects still point to the original object. In other words, only a shallow copy copy the object under consideration, without copying objects it references.

  Deep copy (Deep Clone): all variables are copied object contains the same value as the original target, to remove variables that refer to other objects. Variables that reference other objects will point to the new object is copied, rather than the original objects referenced. In other words, deep copy of the object is copied objects referenced are copied again.

Extended:

  原型模式主要用于对象的复制,实现了接口(实现Cloneable接口),重写一个方法(重写Object类中的clone()方法),即完成了原型模式。

  原型模式中的拷贝分为“浅拷贝”和“深拷贝”:

    浅拷贝:对值类型的成员变量进行值的复制,对引用类型得成员变量只复制引用,不复制引用的对象。

    深拷贝:对值类型的成员变量进行值的复制,对引用类型的成员变量也进行引用对象的复制。

  (Object类的clone方法只会拷贝对象中的基本数据类型的值,对于数组、容器对象、引用对象等都不会拷贝,这就是浅拷贝。如果要实现深拷贝,必须将原型模式中的数组、容器对象、引用对象等另行拷贝。)

  原型模式的优点:

    1.如果创建新的对象比较复杂时,可以利用原型模式简化对象的创建过程。

    2.是永远是模式创建对象比直接new一个对象在性能上要好得多,因为Object类的clone方法是一个本地方法,它直接操作内存中的二进制流,特别是复制大对象时,性能的差别非常明显。

  原型模式的使用场景:

    因为以上优点,所以在需要重复地创建相似对象时可以考虑使用原型模式。比如需要在一个循环体内创建对象,假如对象创建过程比较复杂或者循环次数很多的话,使用原型模式不但可以简化创建过程,而且可以使系统的整体性能提高很多。

Guess you like

Origin www.cnblogs.com/HuiH/p/11926814.html