wood burning stoves
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

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "parameter passing" Watch "parameter passing" New topic

parameter passing

Nazma Panjwani

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


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.

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é

Joined: Nov 24, 2005
Posts: 14688

Check this FAQ.

[My Blog]
All roads lead to JavaRanch
I agree. Here's the link: http://aspose.com/file-tools
subject: parameter passing
jQuery in Action, 3rd edition