• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array

 
Arpana Rai
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arpana Rai
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx a lot...now the concept clear to me.
 
Vivek Talyan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arpana Rai
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic