Daniel Forhan
Computer Science Teacher and Java Programmer

Arrays: Copy vs. Clone

Thursday, December 01, 2005 4:57 PM

Occasionally when working with an array is may be useful to make a copy of that array. What might not be apparent is that by making a copy of an an array there are implications that can cause unintended side-effects if one isn’t aware of what actually happens when a simple copy the array is made.

 

For instance let’s consider:

 

         int [] numbers = { 2, 3, 4, 5};

         int [] numbersCopy = numbers;

 

The “numbersCopy” array now contains the same values, but more importantly the array object itself points to the same object reference as the “numbers” array.

So if I were to do something like:

 

          numbersCopy[2] = 0;

         

 What would be the output for the following statements?

 

          System.out.println(numbers[2]);

          System.out.println(numbersCopy[2]);

 

Considering both arrays point to the same reference we would get:

0

0

for an output.

 

But what if we want to make a distinct copy of the first array with its own reference? Well in that case we would want to clone the array. In doing so each array will now have its own object reference. Let’s see how that will work.

         

          int [] numbers = { 2, 3, 4, 5};

          int [] numbersClone = (int[])numbers.clone();

 

The “numbersClone” array now contains the same values, but in this case the array object itself points a different reference than the “numbers” array.

So if I were to do something like:

         

          numbersClone[2] = 0;

 

What would be the output now for the following statements?

 

          System.out.println(numbers[2]);

          System.out.println(numbersClone[2]);

 

You guessed it:

4

0

 

I spent some time going over this with my class but was not satisfied that we nailed this concept down very well, so I created a simple application using J# (that my student can eventually build) that I think may help illustrate this point further. It dives a little deeper into the basic mechanics of the Java Code with respect to copying or cloning arrays. So here it is. Give a try and see if it helps you draw conclusions about this topic.

 

I started with a simple Windows application as I did in my previous post. This example uses Strings but you could have also coded it for other types.

 

 

 The idea is to compare the hash code that is generated for each array object, and compare those values.  If the hash code values are the same then we can say that the two arrays point to the same object reference.  What is interesting about this program is what it illustrates when you continue to click the “Clone Array” button and then the “Hash Code” button that corresponds to it. Compare that with what happens when you make copies of the array, and check the hash code. You can download the whole program here.

 


Feedback

# re: Arrays: Copy vs. Clone

I like the explanation. Thanks a bunch!!

blacklocist@gmail.com 3/10/2006 2:37 PM | Lee Hicks

# re: Arrays: Copy vs. Clone

A very good explanation. Thank you very much. 5/11/2006 7:55 PM | FelipeNaranja

# re: Arrays: Copy vs. Clone

perfect explanation of what i wanted to find out.

Well done :) 5/15/2006 5:59 PM | Mo

# re: Arrays: Copy vs. Clone

This is excactly the explanation i searched, nice one =) 6/13/2006 9:22 AM | Someone

# re: Arrays: Copy vs. Clone

Nice explanation,
perhaps you can add System.arraycopy() thing as well for comparison.. 6/14/2006 7:28 PM | Afriza

# re: Arrays: Copy vs. Clone

Thanks for the clear explination. 7/12/2006 9:49 AM | GMAT

# re: Arrays: Copy vs. Clone

really very good explanation, thanks a lot 10/22/2006 2:58 PM | neha

# re: Arrays: Copy vs. Clone

Thanks. Very good explanation. I was looking for ways to split an array and I stumbled across this article. Found it very useful. 11/27/2006 5:14 AM | Roger Bert

# re: Arrays: Copy vs. Clone

Very good and clear explaination.... thanxs a lot. 2/7/2007 11:57 AM | Anil

# re: Arrays: Copy vs. Clone

The so called array copy actually is not copying anything except the reference variable, the target array object still keeps the same and of course hash code will be the same. Clone method on the other hand distinctly create another array object with the same array values as the array object to be cloned. Other ways of copying arrays including System.arraycopy() or create another empty array and run the for loop to copy the array values to the empty array. 4/15/2007 4:10 PM | Jacky

# re: Arrays: Copy vs. Clone

Very good explaination 5/7/2007 4:27 AM | Hemanth

# re: Arrays: Copy vs. Clone

Thanks a lot, this precisely answered my question. But what if the array we want to clone is NOT of primitive types? Do we then have to clone the objects too? 5/13/2007 11:36 AM | Huda

# re: Arrays: Copy vs. Clone

How does cloning work for reference types? 5/15/2007 6:32 AM | Raaja

# re: Arrays: Copy vs. Clone

You might think this works, but it's not perfect. It only creates a shallow copy of the object you're trying to clone.

What this means is that it will clone the array by coping references from the first array to the second, but it won't then clone those objects themselves.

This is often what you want, but god forbid that you forget that a multi-dimensional array (e.g. int[][] a = new int[10][10]) is in fact an array of arrays. I stumbled across this problem just recently, and it's a pain. Basically, doing a clone of a two (or more) dimensional array is pretty pointless. And can be an irritating bug. 8/1/2007 3:58 AM | ipsi

# re: Arrays: Copy vs. Clone

its so clear and neatly explained....goog work 8/1/2007 11:51 PM | rach

# re: Arrays: Copy vs. Clone

*good work :) 8/1/2007 11:51 PM | rach

# re: Arrays: Copy vs. Clone

Yup, real good stuff...... 12/11/2007 7:41 AM | Kunal

# re: Arrays: Copy vs. Clone

Hi good explanation.

Well can u just help me.
I have an array
int buf[240000];

Now i have to arrange elements as buf1[ROW][COL]
where ROW = 320
COL = 250
something like a matrix....Can u provide any refernce or any idea?
can reach me at yadraj@yahoo.com 12/20/2007 7:44 AM | bigbee

# re: Arrays: Copy vs. Clone

For those wondering how to achieve deep-copying of multidimensional arrays, instead of writing your own iterative method, consider using Arrays.deepCopy() 5/21/2008 8:22 PM | Usta

# re: Arrays: Copy vs. Clone

Nice explanation, except that you've muddled up your terminology between objects and references. An array is an object. A named variable that refers to an array is a reference. 6/14/2008 5:52 PM | Tim S

# re: Arrays: Copy vs. Clone

Thank you very much 8/16/2008 7:22 PM | Rama

# re: Arrays: Copy vs. Clone

good explanation. Thank you very much 6/16/2009 1:06 AM | Adesh Khare

# re: Arrays: Copy vs. Clone

good explanation 10/23/2009 10:08 AM | harish

Post a comment