wood burning stoves 2.0*
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 OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Array" Watch "Array" New topic
Author

Array

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.


SCJP1.4(91%)
Valentin Crettaz
Gold Digger
Sheriff

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.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Arpana Rai
Ranch Hand

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

Joined: Nov 13, 2002
Posts: 2
Valentin,
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?
thanks,
Vivek
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)
regds
Arpana
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.
regds
Arpana
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array