Shallow clone and deep clone in Java
Summary
The concept of shortest cloning and deep cloning is a relatively low difficulty factor for JavaSE, but it should not be ignored.
Assume that there is no direct operation on a list in the code, but the attributes of the elements in the list are changed. This may involve this concept.
Description
Shortest cloneRefers to the reference that only copies objects in the stack memory ). After copy, the old and new references point to the same heap memory object (that is, the same memory area), but the actual object copy in the heap memory only has one before and after. If you use "=" operator to compare the two addresses, true is returned. (Different references, same object)
Deep cloneIt will copy a new object and return the corresponding reference, that is, it opens up a new heap memory space, so when "=" operator is used to compare the addresses of the two, false will be returned. (Different references, different objects)
Shallow clone)
The explicit clone operations in jdk are basically used:
1 System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
For example, the clone () and Arrays. copyOf () methods in the ArrayList call this method at the underlying layer.
1 package com. scv. test. clone; 2 3 public class ShallowCloneTest {4 5 public static void main (String [] args) throws Exception {6 Zerg z0 = new Zerg (); 7 Zerg z1 = z0; 8 System. out. println ("0. "+ (z0 = z1); //" = "Operator Used for shortest cloning of an object 9 10 Zerg [] array0 = new Zerg [] {new Zerg (), new Zerg ()}; 11 Zerg [] array1 = new Zerg [array0.length]; 12 System. arraycopy (array0, 0, array1, 0, array0.length); // System. arraycopy () is used for the shortest of array 13 System. out. println ("1. "+ (array0 = array1); 14 for (int I = 0; I <array0.length; I ++) {15 System. out. println ("Comparing element of array:" + (array0 [I] = array1 [I]); 16} 17} 18 19} 20 21 class Zerg {22 23} 24 25/* Output: 26 0. true27 1. false28 Comparing element of array: true29 Comparing element of array: true30 */Verify Shallow Clone
Deep clone)
The jdk does not display the definition of deep clone, or does not directly provide a tool class. To make your custom class support deep cloning, you must have two conditions:
If clone () of override Object is not implemented, CloneNotSupportedException is thrown.
1 package com. scv. test. clone; 2 3 public class DeepCloneTest {4 5 public static void main (String [] args) throws Exception {6 CloneableZerg z0 = new CloneableZerg (); 7 CloneableZerg z1 = z0.clone (); 8 9 System. out. println ("0. "+ (z0 = z1); 10} 11 12} 13 14 class CloneableZerg implements Cloneable {15 16 @ Override17 public CloneableZerg clone () throws CloneNotSupportedException {18 return (CloneableZerg) super. clone (); 19} 20} 21 22/* Output: 23 0. false24 */Verify Deep Clone
In fact, you can define which member variables (fields) Allow clone and which do not (a little transient ?).
Implementation in jdk: Shortest clone and deep clone in ArrayList
1 package com. scv. test. clone; 2 3 import java. util. arrayList; 4 5 public class ArrayListCloneTest {6 7 public static void main (String [] args) throws Exception {8 CloneTarget t = new CloneTarget (); 9 10 ArrayList <CloneTarget> list0 = new ArrayList <CloneTarget> (1); 11 list0.add (t); 12 ArrayList <CloneTarget> list1 = (ArrayList <CloneTarget>) list0.clone (); 13 list0.get (0 ). setFieldA (20); 14 15 System. out. println ("0. "+ (list0 = list1); 16 System. out. println ("1. "+ (list0.get (0) = list1.get (0); 17 System. out. println ("2. "+ list1.get (0 ). getFieldA (); 18} 19 20} 21 22 class CloneTarget implements Cloneable {23 24 private int fieldA = 10; 25 26 @ Override27 public CloneTarget clone () throws CloneNotSupportedException {28 return (CloneTarget) super. clone (); 29} 30 31 public void setFieldA (int a) {32 fieldA = a; 33} 34 35 public int getFieldA () {36 return fieldA; 37} 38} 39 40/* 41 * Output: 42*0. false 43*1. true44 * 2. 2045 */Click Me
Operation instructions:
Result description: