File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Array" Watch "Array" New topic


Arpana Rai
Ranch Hand

Joined: Nov 12, 2002
Posts: 93
what will be the output of compiling and running the following program?
class CloneTest
public static void main(String[] args)
int ia[ ][ ] = { { 1 , 2}, null };
int ja[ ][ ] = (int[ ] [ ])ia.clone();
System.out.print((ia == ja) + " ");
System.out.println(ia[0] == ja[0] && ia[1] == ja[1]);
guess what??i get the wrong ans. .....correct ans is "false true". ...can any one explain me plzz.

Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
When you clone the array object referenced by ia, you get a new reference (ja) to that array, thus ia==ja yields false since both references (ia and ja) are not the same. However, the referenced array is the same. Actually there is only one array object in this code example, but it is referenced by two different variables. Since the same array is referenced, ia[0] is the same array component as ja[0], the same for the second array component.
We say that clone() does a shallow copy of the array, that is it does not duplicate the array itself, it only returns a new reference to the array.

[Blog] [Blogroll] [My Reviews] My Linked In
Arpana Rai
Ranch Hand

Joined: Nov 12, 2002
Posts: 93
Thanx a the concept clear to me.
Vivek Talyan

Joined: Nov 13, 2002
Posts: 2
the clone method returns a new object with the parameters copied from the original object to the clone object.
If this is correct then ia and ja are not referring to the same object but the data members in ia and ja are referring to the same objects.
Is this correct?
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
ia and ja refer to different int[][] array objects.
However, both arrays have the same contents, since clone() does only a shallow copy.
ia[0] and ja[0] are both the same reference to the
same int[] array -- the one that contains {1, 2}.
I find Valentin's explanation confusing.

Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Arpana Rai
Ranch Hand

Joined: Nov 12, 2002
Posts: 93
I am confused again.. ...wat actually clone() method do???Does it give new refrence of the same object which is cloned(as said by Valentin) or it gives the duplicate oject as well as its seprate refrencs(as told by Ran)
Arpana Rai
Ranch Hand

Joined: Nov 12, 2002
Posts: 93
I found the follwing statement in JLS
A clone of a multidimensional array is shallow, which is to say that it creates only a single new array. Subarrays are shared.

what does this actually means.Can any one explain me with eg.
I agree. Here's the link:
subject: Array
Similar Threads
what is the Output?
clone method
clone() with arrays??