System.arraycopy() is a shallow copy method.
For an array of a primitive type, it will copy the values from one array to another:
int[] a = new int[]{1,2};
int[] b = new int[2];
System.arraycopy(a, 0, b, 0, 2);
b will be {1,2} now. Changes in a will not affect b.
For an array of a non-primitive type, it will copy the references of the object from one array to the other:
MyObject[] a = new MyObject[] { new MyObject(1), new MyObject(2)};
MyObject[] b = new MyObject[2];
System.arraycopy(a, 0, b, 0, 2);
b will contain a reference of new MyObject(1), new MyObject(2) now. But, if there are any changes to new MyObject(1) in a, it will affect b as well.
Are the above statements correct or not?
Yes, you are correct.
System.arraycopyalways does a shallow copy, no matter if the array contains references or primitives such asints ordoubless.Changes in array
awill never affect arrayb(unlessa == bof course).Depends on what you mean. To be picky, a change to the
new MyObject(1)will neither affectanorb(since they only contain references to the object, and the reference doesn’t change). The change will be visible from bothaandbthough, no matter which reference you use to change the object.