java development --Cloneable interfaces, clone () method and depth of copies

1, implement the Cloneable interface denotes that an object class is to allow cloning.

2, is meant to allow cloning: can be called clone () method.

3, a shallow or deep copy copies, depending on how the override clone Object () method.

4, the relationship between the original object and the clone:

     Deep copy: Perfectly normal paths and difficult path;

     Shallow copy: lingering.

 

 The second point above explanation, if it does not implement Cloneable call clone () method throws an exception. Look Object source to know:

1 protected Object clone() throws CloneNotSupportedException {
2         if (!(this instanceof Cloneable)) {//这里会检查是否是Cloneable的实例
3             throw new CloneNotSupportedException(
4                     "Class " + getClass().getName() +" doesn't implement Cloneable");
5         }
6  
7         return internalClone();
8     }

 

The following give an example of a deep copy:

ArrayList's clone () method:

1   // deep copy 
2   public Object clone () {
 . 3          the try {
 . 4              ArrayList V = (ArrayList <?>) <?> Super .clone ();
 . 5              // new open memory space to the subject a member of the ArrayList Object [] elementData of; 
. 6              v.elementData = Arrays.copyOf (elementData of, size);
 . 7              v.modCount = 0 ;
 . 8              return V;
 . 9          } the catch (CloneNotSupportedException E) {
 10              // the this Should Not happen, Operating since WE are the Cloneable 
. 11              the throw  new new an InternalError (E);
12         }
13     }
14 //transient Object[] elementData;

So get a new ArrayList object, it is a completely independent objects, including object properties and members of the original object does not have any contact. You take your perfectly normal paths, I walk my plank bridge, after you do everything not affect me. This is a deep copy.

If the above deep copy into shallow copy, will become:

1   // shallow copy 
2   public Object clone () {
 . 3          the try {
 . 4              the ArrayList V = (the ArrayList <?>) <?> Super .clone ();
 . 5              // have a common object members elementData, it is to connect two objects the culprit. 
. 6              v.elementData = elementData of;
 . 7              v.modCount = 0 ;
 . 8              return V;
 . 9          } the catch (CloneNotSupportedException E) {
 10              // the this Should Not happen, Operating since WE are the Cloneable 
. 11              the throw  new new an InternalError (E);
 12 is         }
13     }
14 //transient Object[] elementData;

Using the above shallow copy of this clone () method, the respective members of the object and the original object reference the same clone elementData memory address, lingering mutual influence. You're hurt, I am sad; you have a wonderful time, I will silently bless you; you want me, I also want you. This is a shallow copy.

NOTE: If the object A member property is their definition of the type (denoted as B), A deep you want to copy, need to rely on a deep copy clone B () method.

Reference Books: Effective Java (2nd Edition)

Guess you like

Origin www.cnblogs.com/jianghengsh/p/11039557.html