Does calling clone() on an array also clone its contents? Does calling clone() on an array also clone its contents? java java

Does calling clone() on an array also clone its contents?


clone() creates a shallow copy. Which means the elements will not be cloned. (What if they didn't implement Cloneable?)

You may want to use Arrays.copyOf(..) for copying arrays instead of clone() (though cloning is fine for arrays, unlike for anything else)

If you want deep cloning, check this answer


A little example to illustrate the shallowness of clone() even if the elements are Cloneable:

ArrayList[] array = new ArrayList[] {new ArrayList(), new ArrayList()};ArrayList[] clone = array.clone();for (int i = 0; i < clone.length; i ++) {    System.out.println(System.identityHashCode(array[i]));    System.out.println(System.identityHashCode(clone[i]));    System.out.println(System.identityHashCode(array[i].clone()));    System.out.println("-----");}

Prints:

4384790  43847909634993  -----  1641745  1641745  11077203  -----  


If I invoke clone() method on array of Objects of type A, how will it clone its elements?

The elements of the array will not be cloned.

Will the copy be referencing to the same objects?

Yes.

Or will it call (element of type A).clone() for each of them?

No, it will not call clone() on any of the elements.


1D array of primitives does copy elements when it is cloned. This tempts us to clone 2D array(Array of Arrays).

Remember that 2D array clone doesn't work due to shallow copy implementation of clone().

public static void main(String[] args) {    int row1[] = {0,1,2,3};    int row2[] =  row1.clone();    row2[0] = 10;    System.out.println(row1[0] == row2[0]); // prints false    int table1[][]={{0,1,2,3},{11,12,13,14}};    int table2[][] = table1.clone();    table2[0][0] = 100;    System.out.println(table1[0][0] == table2[0][0]); //prints true}