Why clone() is a bad idea for copying array elements?


I have had many questions from young java developers asking why can’t the use the clone() methid even though its part of the Object class. Whenever they call this method for making copies f teh object they end up getting a CloneNotSupportedException.

First lets do some ground work explaining how the clone() method is used in Java. First of all not all object’s can be cloned. Java only allows an object to be cloned if that object’s class has explicitly declared that the object is cloneable by implementing the cloneable interface. If you call the clone() method for a non-cloneable object you end up getting the CloneNotSupportedException.

 The second that we need to understand is that the clone() by default creates a shallow copy of the object i.e., the copied object contains copies of all the primitive values and references in the original object. In other words the references are copied not cloned. clone() doesn’t recursively makes copies of those objects. A class may need to override this default behaviour by overriding the clone() method and making a deeper copy when required. To understand the shallow copy behaviour lets consider making a copy of two-dimensional array of arrays.

     int[][] data = {{1, 2, 3}, {4, 5}};
     int[][] copy = (int[][])data.clone();

     copy[0][0] = 54;                           // this changes data[0][0] too.
     copy[1] = new int[]{75, 65, 84};           // this doesn't change data[1]

So why this strange behaviour? Befoire answering this question let us understand how a the declared array is stored in memory? For those of you who have a background in C/C++ must know that the the first element of an array actually stores teh referenceof that array. And this is actually whats going on. Lets consider another way of how the above mentioned array can be declared whcih greatly simplifies the point:

     int[][] data = new int [2][];
     data[0] = new int[]{1, 2, 3};
     data[1] = new int[]{4, 5};

Now when you try to get the element at [0][0] index in the array, the call actually gets resolved like this:
data.getElementAt(0).getElementAt(0). This is for your understanding only. So in the first getElementAt() call you get the reference for the inner array and then the second call returns the element at the zewroth position. What this essentially means that if you change the values of t

If you want to make a copy of a multi-dimension array you need to make a copy of every dimension explicitly like:

     int[][] data = {{1, 2, 3}, {4, 5}};
     int[][] copy = new int[data.length][];

     for (int i=0; i

Advertisements
Why clone() is a bad idea for copying array elements?

One thought on “Why clone() is a bad idea for copying array elements?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s