• 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

Passing Array References, javaprepare Q.30

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

Hi,
This code compiles and prints 1.
my doubt is we are passing array reference to the change_i method, why don't the changes made on the reference doesn't affect the actual array. why doesn't the value of i[0] changes to 2.

I thought arrays are treated as object refrence when we pass the whole array.
Can anyone explain?
Thanks,
Vanitha.
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code:
public static void change_i(int i[]) {
int j[] = {2};
i = j;
}
Changes the array that is stored in the LOCAL VARIABLE i, the local variable now points to a new array but all is lost when the method returns.
Try this:
change_i( int[] i ){
i[0] = 2 ;
}
Bill
 
Amit Madan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
run this code you will get whatever you want
public class example {
int i[] = {0};
public static void main(String args[]) {
int i[] = {1};

System.out.println(change_i(i));
}
public static int change_i(int i[]) {
int j[] = {2};
i = j;
return i[0] ;
}}
 
Anshul Manisha
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Vanitha
As I see it while passing to a method object reference is passed. What you are trying to do is change the reference of object by assing j to i inside your method. Though it will work well inside your method but when the method exits the calling code will retain the earlier object reference. This is true for any object.
Before you asked this question I thought that I could pass an object as a parameter to a method and change the reference for that object in method and get the changed reference from the calling point. But, I played with it a bit and find how awfully inadequate my understanding was.
Following is an example which might help you get a better grip.

and the output
anshul.JTest15@256a7c
before first
Inside anshul.JTest15@720eeb
outside anshul.JTest15@256a7c
string second

so if you had code like i[0] = 1 inside your method it would have been reflected but what you did was change object reference to object pointed by j.
Thanks
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anshul,
Thanks for your reply.
Vanitha.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic