-1

I was wondering whether the copyOf method in the Arrays class creates new objects which it then passes onto the copied array or just creates new variables that refer to the existing objects. I had tried to construct a bit of code that tests this:

import java.awt.Point;
import java.util.Arrays;

public class PointTest2 {

  public static void main(String[] args) {
  Point[] array1 = new Point[2];
  Point[] array2 = new Point[2];

  for(int i = 0; i < array1.length; i++) {
    array1[i] = new Point(i, i);
  }

  array2 = Arrays.copyOf(array1, 2);
  array1[0] = array1[1];

  array1[1] = array2[0];

  System.out.println(Arrays.toString(array1));
  System.out.println(Arrays.toString(array2));
  System.out.println(array1[0] == array2[1]);
  }
}

This program returns:

[java.awt.Point[x=1,y=1], java.awt.Point[x=0,y=0]]

[java.awt.Point[x=0,y=0], java.awt.Point[x=1,y=1]]

true

It seems that if copyOf only passed references then the resulting arrays should have both elements being a reference to a point object with x = 1 and y = 1. However, I would think that if the copied array were given new copies of the objects then the equals operator should return false since the references should be different. Can someone help me understand what is going on here? Thanks in advance!

Yahya
  • 13,349
  • 6
  • 30
  • 42
  • 1
    "It seems that if copyOf only passed references then the resulting arrays should have both elements being a reference to a point object with x = 1 and y = 1." Why? You're not modifying `array2`... Your logic is unclear to me. – Jon Skeet May 19 '17 at 18:58
  • 1
    the [**java documentation**](https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#copyOf(T[],%20int)) states _the two arrays will contain identical values._ meaning _only_ the references are copied. Maybe this answers your question [**Does Arrays.copyOf produce a shallow or a deep copy?**](http://stackoverflow.com/questions/18351726/does-arrays-copyof-produce-a-shallow-or-a-deep-copy) – Ousmane D. May 19 '17 at 18:59
  • 2
    No, it does not perform a deep copy. It copies the references. Then you're modifying those references in `array1`. – Andy Thomas May 19 '17 at 19:01
  • I see. Is there any programming language where assigning a variable, say x, to another variable, say y, doesn't assign the value of y to x, but rather a reference to y? – Brandon Sweeting May 19 '17 at 19:06
  • 1
    You could also just use `System.identityHashCode()` on each of the array items and see that they refer to the same objects. – Crusha K. Rool May 19 '17 at 19:06

1 Answers1

0

Arrays.copyOf(T[] original, int newLength) method create a new array with new length.but copy the value to new array.So it is shallow copy.If you are storing object type then they will be same.

Source code

public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
        @SuppressWarnings("unchecked")
        T[] copy = ((Object)newType == (Object)Object[].class)
            ? (T[]) new Object[newLength]
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
        System.arraycopy(original, 0, copy, 0,
                         Math.min(original.length, newLength));
        return copy;
    }
gati sahu
  • 2,576
  • 2
  • 10
  • 16