aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes passing array in to method...doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "passing array in to method...doubt" Watch "passing array in to method...doubt" New topic
Author

passing array in to method...doubt

Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362


output is 1,3....

and





In case of first code i thought that output is 3,1.
because array is also object and when it is passed to method then method parameter points to the original object..which is similar when we are passing an object reference variable in to a method.

but why i got this output?

explain 2nd code also?


can anyone clarify me?





SCJP5 and SCWCD1.5
Think Twice Act Wise...
Ravikanth kolli
Ranch Hand

Joined: Feb 10, 2008
Posts: 179

have you seen here


-kolli
Christina Pong
Greenhorn

Joined: Sep 21, 2008
Posts: 8
Hi,
the parameters you passed into method m1(int[] i1, int[] i2) are local variables, so whatever they are doing inside the method m1, is not going to affect the original i1, i2 which are defined in the main method. That's what happens in the code1.
However, in code2, the elements of two arrays really swap; i1[0] becomes 3, i2[0] becomes 1, that's why output is 3,1

I hope above is clear for your question. I had the similar problem before too :-)
pradeepta chopra
Ranch Hand

Joined: Jul 05, 2008
Posts: 137
hi
in code 1:





you have passed a COPY of the refernce variables i1 and i2 in m1(i1,i2).
now these are COPIED into the m1(int[] i1,int[] i2) i1 and i2.These are actually the bit pattern for referencing the arrays.
here in m1(),you are changing the copied bit patterns in the method local variables.
but when the m1() completes, the original i1 and i2 still have their original copies with them.
So,the i1 i2 still refers to the original arrays,and hence the output.

in Code2:




here also you are passing the COPY of the reference variables in the method
which are again being COPIED into m1's i1 and i2.
BUT now you are using these reference bits to actually reach the original array and change the values inside it.


NOTE:
while passing Object references to a method a copy of the reference is passed which can be used to change the values of the object.
The original reference remains the same.
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
hi pradeepta.
Is your NOTE true?
pradeepta chopra
Ranch Hand

Joined: Jul 05, 2008
Posts: 137
u can read the following tutorial
http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
public class PassRef
{
int i=10;
public static void main(String... a)
{
PassRef pr=new PassRef();
System.out.println("i value of pr:"+pr.i);
pr.change(pr);
System.out.println("i value after change:"+pr.i);
}
void change(PassRef pr1)//pr1 and pr pointing to the same object
{
pr1.i=pr1.i+20;
System.out.println("i value of pr1:"+pr1.i);
pr1=new PassRef();
pr1.i=130;
System.out.println("i value of new object pr1:"+pr1.i);
}
}

output:
i value of pr:10
i value of pr1:30
i value of new object pr1:130
i value after change:30
pradeepta chopra
Ranch Hand

Joined: Jul 05, 2008
Posts: 137
public class PassRef
{
int i=10;
public static void main(String... a)
{
PassRef pr=new PassRef();
System.out.println("i value of pr:"+pr.i); //prints 10
pr.change(pr);
System.out.println("i value after change:"+pr.i);//prints 30
}
void change(PassRef pr1)//pr1 and pr pointing to the same object
{
pr1.i=pr1.i+20; // using pr1 value of original pr.i changed
System.out.println("i value of pr1:"+pr1.i);//prints 30
// until here pr1 and pr refering to same object

pr1=new PassRef();//a new PassRef Object is created and assigned to pr1
pr1.i=130;// the value of i for the new object is set to 130
System.out.println("i value of new object pr1:"+pr1.i);//prints 130
}
}

output:
i value of pr:10
i value of pr1:30
i value of new object pr1:130
i value after change:30
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
i am agree with you but in your note The original reference remains the same..
i am not getting this line.
the modifications which occurs in the method also effect the original object.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: passing array in to method...doubt
 
Similar Threads
Array Behaviour
Arrays
Array assignment -- need help
Passing an Array Reference
ARRAY doubt