• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Passin an array to a method - Pass By value or Pass by reference?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I came across this question on Jquest exam (I think):

what is printed out when the following code is executed?
code:
java Mystery Mighty Mouse
1. class Mystery {
2. public static void main(String args[]) {
3. Changer c = new Changer();
4. c.method(args);
5. System.out.println(args[0] + " " + args[1]);
6. }
7. static class Changer
{
8. void method(String s[])
{
9. String temp = s[0];
10. s[0] = s[1];
11. s[1] = temp;
12. }
13. }
14. }
end of code.
a) Mighty Mouse
B) Mouse Mighty
C) code fails o compile
d) code compiles but runtime exception is thrown
I thought the answer was a) but it actually is B) . (I ran the code too). But I didn't understand why. I thought the array s in 'method' is local and so no changes will get reflected in the calling program. I tried passing different types to the method - primitives,wrapper classes like integer etc and in none of the cases did the value change in the calling program.It changes only in the case of an array being passed.
Can anyone provide an explanation ?
Vivek
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vivek,
Good question. I got the same confusion before.
Remember, call by array pass reference, call by primitive type passing value.
public class Test{
...
public static void main(String args[]){
int i = 10;
Changer c = new Changer(i,args);
System.out.println("this is i :"+i+" in main.");
System.out.println(args[0] + "" + args[1]);
}
}
static class Changer{
void method(int i, String s[]){
int iC = ( i-1);
...//as yours
}
}
the output is: this is i: 10 in main
Mouse Mighty
i passed by value, array passed by reference.
Hopefully, it helps.
I suppose if you pass a class in arg, still pass by referece(this point is I guess, but I believe it is true).
Simon
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
arrays are normally passed to the method by reference but primitive data types normally passed by value

Originally posted by Vivek Shinde:
Hi
I came across this question on Jquest exam (I think):

what is printed out when the following code is executed?
code:
java Mystery Mighty Mouse
1. class Mystery {
2. public static void main(String args[]) {
3. Changer c = new Changer();
4. c.method(args);
5. System.out.println(args[0] + " " + args[1]);
6. }
7. static class Changer
{
8. void method(String s[])
{
9. String temp = s[0];
10. s[0] = s[1];
11. s[1] = temp;
12. }
13. }
14. }
end of code.
a) Mighty Mouse
B) Mouse Mighty
C) code fails o compile
d) code compiles but runtime exception is thrown
I thought the answer was a) but it actually is B) . (I ran the code too). But I didn't understand why. I thought the array s in 'method' is local and so no changes will get reflected in the calling program. I tried passing different types to the method - primitives,wrapper classes like integer etc and in none of the cases did the value change in the calling program.It changes only in the case of an array being passed.
Can anyone provide an explanation ?
Vivek


 
Vivek Shinde
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Simon and Ameen
Thanks for the replies. I did know that primitives (int, float etc) pass by value but what about classes ? When I passed Integer to c.method() and changed the value , the changed value was not reflected in the calling program. does this mean that Classes also pass by value ?
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How did you change that Integer value. Integer is immutable.
You must have created another Integer.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Please remember that Java does not pass any parameter directly; it always uses a copy.
For a primitive type a copy of the value is passed. When the value is changed in a method the original is not affected; only the copy is changed.
For an Object a copy of the reference is passed. A 'reference variable' contains a pointer to the object in memory. A copy of the reference variable still points to the original object so any changes made in the method affect the original object BUT if, in the method, you assign the reference variable to a different object; the original reference does not change and, as the variable now points to a different object in memory, the original object is not affected.
A variable designating an array is treated as a 'reference variable'.
Hope that helps.
Jane
 
Vivek Shinde
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks h33 and Jane
What you said did clear a lot of things.
Vivek
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jane,
I fully agree with you. Your above explanation is correct for the case I am giving below:
class Mystery {
public static void main(String args[])
{
Mystery mys = new Mystery();
Changer ch = new Changer();
System.out.println("value of i before method call "+ ch.i); // prints 10
mys.method(ch);
System.out.println("value of i after method call"+ ch.i);
}
public void method( Changer obj)
{
obj.i = 25; // this is visible
obj = null; // this must have no affect
}
}
class Changer
{
int i = 10;
}

Now let us go to the method given by Vivek,
8. void method(String s[])
{
9. String temp = s[0];
10. s[0] = s[1];
11. s[1] = temp;
12. }
Assuming that here also the references are passed by value
the affect of s[0] = s[1] and s[1] = temp; should actually have no affect on the original args[].
No my expalanation for the above real Mystery is as below:
String are objects
Arrays are also passed as references.
when we pass an array if string objects we are passing something like a double pointer ( pardon for my C inclination ) : a kinda
reference to a reference.
Which means that we are passing an array of references and since as array allows us to change the value of its each individual element ( which is a reference in this case ) we are able to
change the ref values of s[0] and s[1] inside method and still see the change out side the method call.
...Pl correct me if I am wrong/
Sasi


[This message has been edited by sasi dhulipala (edited January 10, 2001).]
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sasi,
You're right. If you pass a reference object you can change the original object values from the method.
If the object is an array, and the array values happen to be object references then you can make them point to different objects.
At least, I'm fairly certain of this not able to compile an example at the moment.
Jane
[This message has been edited by Jane Griscti (edited January 10, 2001).]
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please take a look at this discussion about the same topic.
Ajith
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic