• 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

JQplus

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Again stuck with the code from Jqplus.Can somebody plz explain this....
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]);
}
}

Answer is false ,true when run
 
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ia.clone() makes a new Object , with type same as ia , int[][] tht is , and with state same as ia , { { 1 , 2}, null } , tht is , and resturns a reference to this new object , which is stored in ja
now ia == ja compare these object-references , (or array-references ) with each other , and because they are not equal , it returns false

ia[0] == ja[0] && ia[1] == ja[1] compares the values of the array-elements , which are equal , because 'ja' is a clone of 'ia' ( as per previous explanation ) , so it returns true
the trick is when array dimensions are increased from 1 , means multi-dimensional arrays , then instead of storing the whole array as element ( as in array of array ) , only reference to it is stored ,
like for , first element of given array , { 1 , 2 ) , the ia[0] only conatins a reference to this array in heap , and while cloning , only this reference is copied , new copy of whole { 1 , 2 ) is not made , so ia[0]==ja[0] returns true , because this is effectively comparing reference to same element ( {1,2} tht is )
well..i knw is hard to understand with my explanation , so i quote a line from JLS


A clone of a multidimensional array is shallow, which is to say that it creates only a single new array. Subarrays are shared


refer u to section 10.7 of JLS 2nd ed for more on this
------------------
Gagan (/^_^\)
 
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent effort Gagan!
Originally posted by Gagan :


the trick is when array dimensions are increased from 1 , means multi-dimensional arrays , then instead of storing the whole array as element ( as in array of array ) , only reference to it is stored....clipclipclip...


Now have a whisky from me. Should the word not be same instead of equal?
Originally posted by Gagan :


now ia == ja compare these object-references , (or array-references ) with each other , and because they are not equal, it returns false



------------------
azaman
[This message has been edited by Ashik uzzaman (edited August 16, 2001).]
 
Gagan Indus
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ashik
well.. i am not tht gud at finding right-words
but still i think tht in this case word shld be equal and NOT same
ia points to a object
ja points to clone of the above object
the state of both objects is same
but ia and ja , which are references to these objects , are not equal to each other
so ia and ja are not equal when compared
ia==ja

well.. is not equal or same is more of a english-literature discussion , than java-lingo discussion ...lol
BTW , thnakx for the drink !
( r nt u too generous at offerning drinks ..lol )
( hey ! i hav a lill q , if u r drinking whisky , and i am drinking whisky too , then are we having the same drinks , or its just tht our tastes are equal !!..lol )
------------------
Gagan (/^_^\)
 
Then YOU must do the pig's work! Read this tiny ad. READ IT!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic