• 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

array ref. confusion

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hii, given the following code:wht will be the output?
answer given is 1. But i think '2'. can anyone explain?

public class example {

int i[] = {0};

public static void main(String args[])
{
int i[] = {1};
change_i(i);
System.out.println(i[0]);
}
public static void change_i(int i[]) {
int j[] = {2};
i = j;
}
}
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ana,

What happens here is that the array reference that you pass to the method is copied to the method's array reference parameter. So even if you change the method's parameter (in this case reassigning it to a different array object) that doesn't affect the original array reference, which remains pointed to the original array object.

You could have changed the value of one of the array elements in the method, and that would have changed the original array contents though.

Remember that array variables are reference variables which point to array objects.
 
anu sav
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks ruben,
but what i am thinking is when we pass array referece as a argument to function,
whatever changes we made to that ref. get reflected in main.isn't it ?
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ana sav wrote:thanks ruben,
but what i am thinking is when we pass array referece as a argument to function,
whatever changes we made to that ref. get reflected in main.isn't it ?


Hi Ana,

No, actually that's not the way it works. When you have a method that takes an array reference as an argument, for example:

And you call that method this way:

What happens is that you copy the value of i (which, like any reference variable is just a bit pattern which denotes the location of the array object itself) to the a parameter in the method. That means that both i and a will point to the same array object (and you can even modify the array elements through the reference a.) But if you reassign a itself, it won't affect the array object that reference i points to. It will just make a point to a different array (or possibly set it to null) without affecting i.

Does that make things more clear?
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
everything in java is passed by value...
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

welly tambunan wrote:everything in java is passed by value...


Welcome to the forum!

That is true, but that might be a little confusing to people that come from C++ for example. In Java, the semantics of object variables are different to the semantics in C++. In Java an object variable is a reference to the object, whereas in C++ when you talk of an object variable, the variable itself denotes the object (not a reference to the object.) That's why C++ has pass-by-reference. Just pointing that out in case some folks get confused by the passing by value thing.
 
Welly Tambunan
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ruben Soto wrote:
Welcome to the forum!



Thax ruben. That's very kind of you. Make me feel comfortable. Feels like home.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

welly tambunan wrote:

Ruben Soto wrote:
Welcome to the forum!



Thax ruben. That's very kind of you. Make me feel comfortable. Feels like home.


This is a great forum, I was glad I found it too.
 
anu sav
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks ruben,
little what confused with c++.
now i got it.
 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JAVA is not C++. There are many things in JAVA which are identical to other programming languages but they are distinct. SO don't be confused. And as Ruben said you earlier JAVA is pass-by-value or pass-by-copy.

Also welcome to java ranch.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's correct. If you are not familiar with C++ please disregard my comments
I just had to mention it in case you have previous experience with C++, because if you do, then at first sight Java seems like it's not using pass-by-value, but it actually is.
 
I wasn't selected to go to mars. This tiny ad got in ahead of me:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic