Does System.arrayCopy () copy an object or an object reference?

I have a final NameAndValue class. I copied an array of NameAndValue objects using System.arrayCopy() , and when I changed the NameAndValue object in the copied array, it gets reflected in the original array.

 public final class NameAndValue { public String name; public String value; public NameAndValue() { } public NameAndValue(String name,String value) { this.name = name; this.value = value; } } public class Main { public static void main(String[] args) { NameAndValue[] nv = new NameAndValue[4]; nv[0] = new NameAndValue("A", "1"); nv[1] = new NameAndValue("B", "2"); nv[2] = new NameAndValue("C", "3"); nv[3] = new NameAndValue("D", "4"); NameAndValue[] nv2 = new NameAndValue[4]; System.arraycopy(nv, 0, nv2, 0, 2); nv2[2] = new NameAndValue("Y","25"); nv2[3] = new NameAndValue("Z","26"); for (int i = 0; i < 2; i++) { NameAndValue[] nv3 = new NameAndValue[4]; System.arraycopy(nv2, 0, nv3, 0, 4); nv3[2].value = String.valueOf(i); nv3[3].value = String.valueOf(i+1); System.out.println(nv2[2].value); System.out.println(nv2[3].value); System.out.println("-----------------------"); } } } 

I get a conclusion like,

 0 1 ----------------------- 1 2 ----------------------- 

I had to get a result like 25 and 26 in all cases, right? why is it changing?

+9
java
Feb 28 '13 at 12:04 on
source share
5 answers

Does System.arrayCopy () copy an object or an object reference?

Link, this is a shallow copy. Surprisingly, the documents do not speak explicitly , simply implicitly, since they only talk about copying the elements of the array, and not recursively copying what they reference.

This is exactly the same as if you had this:

 NameAndValue nv1 = new NameAndValue("A", "1"); NameAndValue nv2 = nv1; nv2.value = "4"; System.out.println(nv1.value); // 4 

Each element of the array is similar to nv1 and nv2 vars above. In the same way as nv1 and nv2 reference (point to) the same basic object, as well as array entries, including when these entries are copied from one array to another.

+18
Feb 28 '13 at 12:06 on
source share

It changes because both arrays still refer to the same underlying objects. You can assign a new object to a position in the array, and it will not be displayed in another array, but if you modify the object that both arrays point to, then both will see it differently.

In this sense, it passes a β€œlink by value”, and does not strictly pass by reference, but the effect you see is that the link remains the same.

Copies like this are almost always shallow copy in Java, unless explicitly stated otherwise. This is no exception.

+2
Feb 28 '13 at 12:05
source share

As far as I know, arraycopy is a shallow copy. This means that the new array will refer to the addresses of the elements in the old array. Therefore, if you adjust the value in one of them, it will reflect how

+1
Feb 28 '13 at 12:09
source share

System.arraycopy is a shallow copy. that's why:

it uses JNI to use something like memcpy (), which simply copies pointers in memory. This is a very fast operation.

+1
Dec 03 '13 at 20:31
source share
  int[] a = {1, 2, 3}; int[] b = new int[3]; System.arraycopy(a, 0, b, 0, 3); b[1] = 0; System.out.println(Arrays.toString(b)); System.out.println(Arrays.toString(a)); 

The conclusion is as follows: [1, 0, 3] [1, 2, 3]

0
Aug 12 '16 at 2:44
source share



All Articles