View Single Post
Old 11-04-2003, 05:15 PM   #15 (permalink)
DarkTwilkitri
Registered User
 
Join Date: Feb 2003
Location: Australia
Posts: 21
DarkTwilkitri is on a distinguished road
Actually, this is starting to make sense...
Java always passes by value. But, what we think of as an object is actually a reference...
So when you pass int a = 6 to a function, you're passing a copy of 6 - this copy is altered within the function, and therefore does not affect the original six.
And when you pass Integer intwrapper = new Integer(6) to a function, all intwrapper is, is a reference to the Integer created in memory. So the reference gets copied. So, if you do something which affects the reference, ie:
Code:
void alterInteger(Integer anInt)
{
    anInt = new Integer(7);
}
the value of the object reference anInt is changed to that of the new Integer, without changing the original reference intwrapper. Then it's lost when it loses scope.
But if you do... OK, Integer was a bad example because it doesn't have any set methods. Hmm. Just say that Integer had a setInt method...
Anyway, if you do something which alters something the reference refers to...
Code:
void alterInteger(Integer anInt)
{
    anInt.setInt(7);
}
This will go to the object referenced by the copy anInt, which is the same object referenced by intWrapper, and alter the values inside the object, which will affect both copies since they are merely references to the object.


Of course, there's every chance that I'm completely wrong and I'm just making a fool out of myself. But this is the only conclusion I can draw
DarkTwilkitri is offline   Reply With Quote