• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

passing array in to method...doubt

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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?



 
Ranch Hand
Posts: 179
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
have you seen here
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 :-)
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi pradeepta.
Is your NOTE true?
 
pradeepta chopra
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
u can read the following tutorial
http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Maybe he went home and went to bed. And took this tiny ad with him:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic