File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes JQplus Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "JQplus" Watch "JQplus" New topic
Author

JQplus

Mini Pilla
Ranch Hand

Joined: Jul 15, 2001
Posts: 112
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
Gagan Indus
Ranch Hand

Joined: Feb 28, 2001
Posts: 346
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 (/^_^\)


Gagan (/^_^\) SCJP2 SCWCD IBM486 <br />Die-hard JavaMonk -- little Java a day, keeps you going.<br /><a href="http://www.objectfirst.com/blog" target="_blank" rel="nofollow">My Blog</a>
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370

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).]


Ashik Uzzaman
Senior Member of Technical Staff, Salesforce.com, San Francisco, CA, USA.
Gagan Indus
Ranch Hand

Joined: Feb 28, 2001
Posts: 346
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 (/^_^\)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JQplus