• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

parameter passing

 
Nazma Panjwani
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this FAQ.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic