Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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??

like image 619
prasanth Avatar asked Feb 28 '13 12:02

prasanth


People also ask

What is System Arraycopy () method?

arraycopy() method copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array. A subsequence of array components are copied from the source array referenced by src to the destination array referenced by dest.

Is System Arraycopy a deep copy?

System. arraycopy does shallow copy, which means it copies Object references when applied to non primitive arrays.

Does System Arraycopy create new array?

While System. arraycopy() simply copies values from the source array to the destination, Arrays. copyOf() also creates new array. If necessary, it will truncate or pad the content.

How does array copy work?

Copy(Array, Array, Int32) Copies a range of elements from an Array starting at the first element and pastes them into another Array starting at the first element. The length is specified as a 32-bit integer.


4 Answers

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.

like image 126
T.J. Crowder Avatar answered Oct 19 '22 23:10

T.J. Crowder


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.

like image 34
Michael Berry Avatar answered Oct 20 '22 00:10

Michael Berry


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

like image 32
Toon Casteele Avatar answered Oct 19 '22 23:10

Toon Casteele


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.

like image 22
David T. Avatar answered Oct 20 '22 00:10

David T.