This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Passing an array to a method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Passing an array to a method" Watch "Passing an array to a method" New topic

Passing an array to a method

Malik Tahir

Joined: Sep 29, 2001
Posts: 18
I m getting some problem in this question. Would anyone help me.
Question is
What happens when the following program is compiled and run. Select the one correct answer.

Answer is 1. But i think it should be 2.
Can anyone help me in this question.
Thanks in advance.
Marcus Howarth
Ranch Hand

Joined: Jan 04, 2002
Posts: 38

Rule: Chap 10, JLS: In the Java programming language arrays are objects (�4.3.1), are dynamically created, and may be assigned to variables of type Object (�4.3.2). All methods of class Object may be invoked on an array.
when you pass a reference of an object to another method, you pass a reference to a copy of that object

this must work for arrays too in the same way.
(we'll get a rancher to correct me soon when the US wakes up)


SCJP, March '02
Mick Lennon

Joined: Jan 25, 2002
Posts: 1
I *think* the reason its 1 is because i is passed to change_i by reference as opposed to by value.
Therefore while i[0] = 2 in the method change_i, in the method main, it remains i[0] = 1. If you wanted to "permanently" change the value to 2, you would type i[0] = 2; in change_i (as this would be accessing the object directly rather than the reference)
This is my understanding, which I admit might be completely wrong, so please feel free to correct me.
chafule razgul
Ranch Hand

Joined: Feb 09, 2002
Posts: 63
What got passed into change_i is a copy of the reference to the array. Inside the method this reference is changed. As a result it no longer refers to the original array, i. This reference goes out of scope and (is this right)the array referred by it becomes ready for garbage collection when method change_i completed, and has no bearing on the array i in main the argument of change_i was just a copy of the reference, not the actual reference itself.
hope this is clear
Jian Yi
Ranch Hand

Joined: Feb 01, 2002
Posts: 127
Originally posted by chafule razgul:
What got passed into change_i is a copy of the reference to the array,... not the actual reference itself.

Then why when I use i[0]=2 instead of i=j in change_i(), i[0] in main gets changed to 2?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Jenny Yin:

Then why when I use i[0]=2 instead of i=j in change_i(), i[0] in main gets changed to 2?

Because, initially, the parameter i in the method change_i references the same array as the array i in the main method. If you modify the contents of the i in change_i, you'll be modifying the contents of the array i in main. If, however, you modify the reference to i in change_i directly, as done in the original example, the i in change_i and the i in main will no longer reference the same array.
Take a look at this code:

You can see that, until we change the local variale i, in line 1, we can modify the original array (a) from main. However, after line 1, we can not do that.
I hope that helps,

SCJP Tipline, etc.
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
Because you're changing an element of the array rather than the reference to the array.

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Himanshu Jhamb
Ranch Hand

Joined: Aug 01, 2001
Posts: 134
Hiya Fellow Ranchers !
Believe it or not ! I was about to post this very question before I decided to go through the latest five in this forum.... and guess what ! My question had already been posted... Such is the greatness of this site !
Excellent explanation Corey... it all became clear after reading your explanation.

Himanshu Jhamb<br />SCJP2 v1.2 (April 2002)<br />SCJP2 v1.4 (May 2002)
Jian Yi
Ranch Hand

Joined: Feb 01, 2002
Posts: 127
Thanks, Corey!
I agree. Here's the link:
subject: Passing an array to a method
It's not a secret anymore!