wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Passing an Array Reference 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 "Passing an Array Reference" Watch "Passing an Array Reference" New topic
Author

Passing an Array Reference

shanthisri mocherla
Ranch Hand

Joined: Sep 05, 2005
Posts: 119
Hi friends
I've aquestion here .Can someone help???
My questions
1.Here when i'm assiging a3[1]=7;
the change is visible both in a1 and a2 as a1 =a2 .after the fix(a1) method returns .
the ouput is 15 ,15..




[ September 05, 2005: Message edited by: shanthisri mocherla ]
[ September 05, 2005: Message edited by: shanthisri mocherla ]
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
Shanthisri

Lets break up your code and see whats happening, may be that might answer your questions.

long[] a1={3,4,5};

Now you call the method fix(a1) and pass a1 as an argument, what you are passing is infact a reference to object a1. That reference is picked up by array a3, which you define here long[] fix(long[] a3). So now the picture looks like.

You change a[1]=7, so

Now, you return from method fix() and a2 picks up the reference, so


Yes, arrays are objects in Java that store multiple variables of the same type. Arrays can hold either primitive or object references, but the array itself will always be an object on the heap, even if the array is declared to hold primitive elements.
shanthisri mocherla
Ranch Hand

Joined: Sep 05, 2005
Posts: 119
Thanks Abdulla

I've one more question on array reference passed to the methods
//the output is 1,3


//the output is 3,1

Can anyone expalin why in the first example the values are not changing?
[ September 05, 2005: Message edited by: shanthisri mocherla ]
Roopesh Gulecha
Ranch Hand

Joined: Aug 09, 2005
Posts: 32
Hi Shanthisri,
An easy way to remember arrays and references to them when passed to methods is like this:
1. Ofcourse a copy of the reference (pointing to the array object) is passed when the method m1 in the above examples is called.
2. So references i1 and i2 in the method m1 is different from the references i1 and i2 in the main method. (it is like the array object is a Lock and the references are the Keys to open the Locks - so here we have two sets of Keys that open the same lock and both have the same name but in different methods)
3. As long as the i1 and i2 references in the method m1 are changed (for example i1 = i2 or int[] i3 = i1 etc), it does not affect the original array content (declared in the main method).
4. But if the i1 and i2 references in the method m1 change the individual array element values, it reflects in the original arrays. (For example i1[0] = i2[0] -> this will change the value in the original array declared in the main method).
So look for what is being changed the reference by itself or the array elements pointed by the references.
Hope this helps...and so you can see the results as they are.

Roopesh
SCJP 1.4 in progress.
srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
hi Abdulla,

that waa a very good explanation ..frnds i suppose if we can make diagramatic of any question that wud b very much useful to us ...
once again thanks Abdulla.

thanks & regards

srikanth


Thanks & Regards<br /> <br />-Srikanth
shanthisri mocherla
Ranch Hand

Joined: Sep 05, 2005
Posts: 119
Thanks Roopesh
for your explanation

Now I'm slowly figuring out the references passed to the methods.
I've still some questions on String and StringBuffer references.I shall post them soon .

Shanthi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing an Array Reference