• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Array

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx a lot...now the concept clear to me.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Something must be done about this. Let's start by reading this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic