98

What exactly does the clone() method in Java return when used on an array? Does it return a new array with data copied from the original?

Ex:

int[] a = {1,2,3};
int[] b = a.clone();
Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
Bunsen McDubbs
  • 1,151
  • 1
  • 9
  • 9
  • Go through following link to understand clone(). [clone (Java method)](http://en.wikipedia.org/wiki/Clone_(Java_method)) and [What is cloning](http://geekexplains.blogspot.com/2008/06/what-is-cloning-how-clone-method-works.html). These link will light up the question you asked. – Smit Jan 04 '13 at 01:17
  • This does not appear to be a duplicate of the suggested question, which asks specifically about cloning of objects referenced by an array. This question does not ask that, and its example is an array of primitives. – Andy Thomas Feb 23 '16 at 17:12
  • @AndyThomas if your reopening was justified, please remove the link to the duplicate, otherwise please re-close the Question. – Cœur Jul 08 '18 at 16:07

2 Answers2

163

When the clone method is invoked upon an array, it returns a reference to a new array which contains (or references) the same elements as the source array.

So in your example, int[] a is a separate object instance created on the heap and int[] b is a separate object instance created on the heap. (Remember all arrays are objects).

    int[] a = {1,2,3};
    int[] b = a.clone();

    System.out.println(a == b ? "Same Instance":"Different Instance");
    //Outputs different instance

If were to modify int[] b the changes would not be reflected on int[] a since the two are separate object instances.

    b[0] = 5;
    System.out.println(a[0]);
    System.out.println(b[0]);
    //Outputs: 1
    //         5

This becomes slightly more complicated when the source array contains objects. The clone method will return a reference to a new array, which references the same objects as the source array.

So if we have the class Dog...

    class Dog{

        private String name;

        public Dog(String name) {
            super();
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

    }

and I create and populate an array of type Dog...

    Dog[] myDogs = new Dog[4];

    myDogs[0] = new Dog("Wolf");
    myDogs[1] = new Dog("Pepper");
    myDogs[2] = new Dog("Bullet");
    myDogs[3] = new Dog("Sadie");

then clone dog...

    Dog[] myDogsClone = myDogs.clone();

the arrays refer to the same elements...

    System.out.println(myDogs[0] == myDogsClone[0] ? "Same":"Different");
    System.out.println(myDogs[1] == myDogsClone[1] ? "Same":"Different");
    System.out.println(myDogs[2] == myDogsClone[2] ? "Same":"Different");
    System.out.println(myDogs[3] == myDogsClone[3] ? "Same":"Different");
    //Outputs Same (4 Times)

This means if we modify an object accessed through the cloned array, the changes will be reflected when we access the same object in the source array, since they point to the same reference.

    myDogsClone[0].setName("Ruff"); 
    System.out.println(myDogs[0].getName());
    //Outputs Ruff

However, changes to the array itself will only affect that array.

    myDogsClone[1] = new Dog("Spot");
    System.out.println(myDogsClone[1].getName());
    System.out.println(myDogs[1].getName());
    //Outputs Spot
    //        Pepper

If you generally understand how object references work, it is easy to understand how arrays of objects are impacted by cloning and modifications. To gain further insight into references and primitives I would suggest reading this excellent article.

Gist of Source Code

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • I heard that using `.remove()` won't effect the original array. Only `Mutating ` methods will effect the original array. is that correct ?? – Null Jan 12 '17 at 21:34
0

clone() method creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object. The general intent is that, for any object x, the expression:

 x.clone() != x

Will be true, and that the expression:

 x.clone().getClass() == x.getClass()

Will be true, but these are not absolute requirements.

While it is typically the case that:

 x.clone().equals(x)

will be true, this is not an absolute requirement.

By convention, the returned object should be obtained by calling super.clone. If a class and all of its superclasses (except Object) obey this convention, it will be the case that x.clone().getClass() == x.getClass().

xlm
  • 6,854
  • 14
  • 53
  • 55
9ine
  • 879
  • 2
  • 8
  • 18