| Author |
parameter passing
|
Nazma Panjwani
Greenhorn
Joined: Jan 17, 2010
Posts: 14
|
|
Hi,
When passing an object as a parameter to a method, for ex. an array, I know that a reference to that object is passed in. If in that method, the formal parameter is made to point to a different object, how come the original variable is still pointing to the same object?
Example arrays:
int[] integers = new int [5]
calling method changeSize(integers)
public changeSize (int[] numbers)
int [] temp = new int[numbers.length*2];
copying the values of numbers array into temp array;
numbers = temp;
numbers array is pointing to the new array....but integers array is still pointing to the old array.
I heard in C# you can do this, make integers pointing to whatever numbers is pointing at, but in java you can't??? Why is that?
How does C# accomplish this then??
Thanks
|
 |
W. Joe Smith
Ranch Hand
Joined: Feb 10, 2009
Posts: 710
|
|
In Java you aren't passing the actual reference into the method, rather a copy of the reference. So the numbers reference and the integers reference are 2 seperate references to the same object. If you change the reference in one, it doesn't change the reference in the other.
|
SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
|
Check this FAQ.
|
[My Blog]
All roads lead to JavaRanch
|
 |
 |
|
|
subject: parameter passing
|
|
|