11

I am having a final class NameAndValue. I copied an array of NameAndValue objects using System.arrayCopy() and when I changed a NameAndValue object in 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 am getting output as:

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

I should have got output as 25 and 26 in all the cases, right?? Why does it get changed??

prasanth
  • 3,502
  • 4
  • 28
  • 42

6 Answers6

22

System.arrayCopy() copies object or reference to object?

Reference, it's a shallow copy. Surprisingly, the docs don't say that explicitly, just implicitly as they only talk about copying array elements, not recursively copying the things they reference.

It's 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 array element is like the nv1 and nv2 vars above. Just as nv1 and nv2 reference (point to) the same underlying object, so do the array entries, including when those entries are copied from one array to another.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

It gets changed because both arrays are referencing the same underlying objects still. You could assign a new object to a position in the array and it wouldn't get reflected in the other array, but if you make a modification to the object that both arrays are pointing to, then both will see it differently.

In this sense, it's pass "reference by value", rather than strictly pass by reference - but the effect you're seeing is because the reference remains the same.

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

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
1

As far as I know arraycopy is a shallow copy. Which means the new array will reference to the addresses of the elements in the old array. So if you adjust a value in one of them, it will reflect on both

Toon Casteele
  • 2,479
  • 15
  • 25
1

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

it uses JNI to use something like memcpy() which just copies the pointers in memory. it's a very fast operation.

David T.
  • 22,301
  • 23
  • 71
  • 123
  • In Java 11 (for example), `arraycopy` has the `@HotSpotIntrinsicCandidate` annotation which probably means that it no longer uses JNI. (But it is definitely a native code implementation.) – Stephen C Apr 09 '21 at 00:40
0
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 output is like this:

[1, 0, 3]
[1, 2, 3]
Community
  • 1
  • 1
Wang Bucca
  • 98
  • 5
  • 1
    This just shows that it does a shallow copy. Your structure has no depth. If it did have depth, you'd find that `System.arraycopy` does not do a deep copy. – T.J. Crowder Aug 12 '16 at 06:49
  • Yes, you are right. I tested not changing the reference of an array element, instead, I changed the content of the data referenced. I found these 2 arrays values are the same. thks! – Wang Bucca Aug 12 '16 at 09:47
0

Method System.arraycopy does not perform a deep copy of an array, nor does Arrays.copyOf method. It copies references to objects and does not create new instances. If you modify any object in one of the arrays, it affects both arrays:

public static void main(String[] args) {
    NameAndValue[] arr1 = new NameAndValue[2];
    arr1[0] = new NameAndValue("A", "1");
    arr1[1] = new NameAndValue("B", "2");

    NameAndValue[] arr2 = new NameAndValue[2];
    System.arraycopy(arr1, 0, arr2, 0, 2);
    arr2[0].value = "3";                       // modify existing object

    NameAndValue[] arr3 = new NameAndValue[2];
    System.arraycopy(arr1, 0, arr3, 0, 2);
    arr3[1] = new NameAndValue("Y", "5");      // create new instance

    System.out.println(Arrays.toString(arr1)); // [A:3, B:2]
    System.out.println(Arrays.toString(arr2)); // [A:3, B:2]
    System.out.println(Arrays.toString(arr3)); // [A:3, Y:5]
}
public static final class NameAndValue {
    public String name;
    public String value;

    public NameAndValue(String name, String value) {
        this.name = name;
        this.value = value;
    }

    @Override
    public String toString() {
        return name + ":" + value;
    }
}

See also:
Why does Array.copyOf() mutate the original array in case of 2D Arrays?
How do I do a deep copy of a 2d array in Java?