aspose file tools*
The moose likes Beginning Java and the fly likes parameter passing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "parameter passing" Watch "parameter passing" New topic
Author

parameter passing

Nazma Panjwani
Greenhorn

Joined: Jan 17, 2010
Posts: 16
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: 14687
    
  16

Check this FAQ.


[My Blog]
All roads lead to JavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: parameter passing
 
Similar Threads
Sun Cirtification
The output of the following programs
array instantiation
method overloading in sort program
why the list size is 1