Shallow copy

Java allows you to copy an array variable to another array variable. Both variables reference an array.

Int [] numbers = {1,2,3,4,5};

Int [] luckyNumbers = numbers;

Value references for both arrays correspond to the same heap address:

public class Main {
    public static void main(String[] args) {
        int[] numbers = new int[] {1.2.3.4.5};
        System.out.println("Original array:" + Arrays.toString(numbers));
        int[] luckyNumbers = numbers;
        System.out.println("Shallow copy array:" + Arrays.toString(luckyNumbers));
        luckyNumbers[3] = 1;
        System.out.println("Modify the original array after luckyNumbers:"+ Arrays.toString(numbers)); }} Original array: [1.2.3.4.5] Shallow copy array: [1.2.3.4.5] Modify the original array after luckyNumbers: [1.2.3.1.5]
Copy the code

Deep copy

A deep copy of an array is a copy of all the values in the array into a new array

Leverage the copyOf method of the Arrays class.

public class Main {
    public static void main(String[] args) {
        int[] numbers = new int[] {1.2.3.4.5};
        System.out.println("Original array:" + Arrays.toString(numbers));
        int[] luckyNumbers = Arrays.copyOf(numbers, numbers.length);
        System.out.println("Deep-copy array:" + Arrays.toString(luckyNumbers));
        luckyNumbers[3] = 1;
        System.out.println("Modify the original array after luckyNumbers:"+ Arrays.toString(numbers)); }} Original array: [1.2.3.4.5] deep copy array: [1.2.3.4.5] Modify the original array after luckyNumbers: [1.2.3.4.5]
Copy the code

As you can see, deep copy simply passes the values in the array, not the references.

Arrays. CopyOf source code parsing

    public static int[] copyOf(@NotNull int[] original,@NotNull int newLength) {
        int[] copy = new int[newLength];
        System.arraycopy(original, 0, copy, 0,
                         Math.min(original.length, newLength));
        return copy;
    }

Copy the code

Original: indicates the original array

NewLength: indicates the length of the new array to be copied

The array. copyOf method actually calls a native Java method called System. arrayCopy

Arraycopy ();

/ * * *@param      src      the source array.
  * @param      srcPos   starting position in the source array.
  * @param      dest     the destination array.
  * @param      destPos  starting position in the destination data.
  * @param      length   the number of array elements to be copied.
  * @exception  IndexOutOfBoundsException  if copying would cause
  *               access of data outside array bounds.
  * @exceptionArrayStoreException if an element in the <code>src</code> * array could not be stored into the <code>dest</code> array *  because of a type mismatch. *@exception  NullPointerException if either <code>src</code> or
  *               <code>dest</code> is <code>null</code>.
  */
public static native void arraycopy(Object src,  int  srcPos,
                                        Object dest, int destPos,
                                        int length);
Copy the code
  • SRC: the source array
  • SrcPos: the start position of the source array
  • Dest: destination array
  • DestPos: indicates the start position of the destination array
  • Length: Length of the array element to be copied.