This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes array ref. confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "array ref. confusion" Watch "array ref. confusion" New topic
Author

array ref. confusion

anu sav
Ranch Hand

Joined: Jan 30, 2009
Posts: 47
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

Joined: Dec 16, 2008
Posts: 1032
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.


All code in my posts, unless a source is explicitly mentioned, is my own.
anu sav
Ranch Hand

Joined: Jan 30, 2009
Posts: 47
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

Joined: Dec 16, 2008
Posts: 1032
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

Joined: Aug 18, 2008
Posts: 40
everything in java is passed by value...
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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

Joined: Aug 18, 2008
Posts: 40
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

Joined: Dec 16, 2008
Posts: 1032
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

Joined: Jan 30, 2009
Posts: 47
thanks ruben,
little what confused with c++.
now i got it.
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 818

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.


Regards, Sud.
SCJP 5 ScjpFAQ JLS
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: array ref. confusion
 
Similar Threads
question about arrays
Array - question
doubt in arrays
What is output and Why ?
help!!