Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array ref. confusion

 
anu sav
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • 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;
}
}
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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?
 
Welly Tambunan
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
everything in java is passed by value...
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
thanks ruben,
little what confused with c++.
now i got it.
 
sudipto shekhar
Ranch Hand
Posts: 823
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic