1.1 shallow copy

Shallow copy copies only values, not reference types. That is, a shallow copy of an object in which the reference properties point to the same address object as the original object.

1.2 deep copy

Deep copy copies not only base type variables, but also reference type variables. Java does not display support for deep copy, but it can be implemented in variants such as the Clone method, serialization, collections.copy (), and so on.

  • Collections.copy()

Only values from the original set are assigned to the destination set, using override. This requires that the length of the destination set be no smaller than the original set. According to the conclusion above, we can create a new empty set with the same size as the original set as the target set.

ArrayList<Integer> myCopy =new ArrayList<Integer>(); mycopy=(ArrayList<Integer>) vec.clone(); ArrayList<Integer> myCopy =new ArrayList<Integer>(array.asList (new Integer[vec.size()])); Collections.copy(mycopy, vec);Copy the code

1.3 Delayed Copy

Delayed copy is a combination of shallow copy and deep copy. When you first copy an object, you use a shallow copy that is faster, and you use a counter to keep track of how many objects share the data. When the program wants to modify the original object, it decides whether the data is shared (by checking counters) and makes deep copies as needed.