Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Wrapper not sending by reference?

Started by Steers Jan 11, 2005 at 9:20 PM 4 replies 1.3k views
Original Post
Steers
Steers
Howdy yall, I have a little problem. I can't use instance variables and i need to pass by reference. So i can't use primitive data types since they are not passed by reference. I wrapped them up in a wrapper class. I have two Integer types that i send to a method. Once its in the method everything is fine. In the method i sent it to, i send it to another method as Integer (the same wrapper) so there is no change its still a wrapped integer. I do my logic on it and change its value...at the end of that method i check the value. It has value, some number that changes everytime(as it is suppose to do). However right after i check the value, i have a return. Right after i return, the values are reset to 0. But they are wrapped in a class. They should be sending by reference. Take a look at this example that demonstrates the situation.

void main()
{
  //
  Integer intOne;
  Integer intTwo;
  firstMethod(intOne, intTwo);
}

void firstMethod(Integer intOne,Integer intTwo)
{
  //display stuff
  ...
  getValues(intOne, intTwo);
  //right after we return
  //display Integers
  //they are 0 now :(
}

void getValues(Integer intOne,Integer intTwo)
{
  //display the integers...everythings good
  //do stuff, change values
  //right before we return...display new values...yay the Integers have changed
  return;
}
In firstMethod it SHOULD have the changed values because classes are passed by reference, but for some reason, it isnt. The values of the class are changed...it returns, then they are back to 0. There is no code inbetween to change them. The only possible explanation is that it is not passing by reference, but i dont know why. Any help would be greatly appeciated, thank you.
capn_midnight
capn_midnight
Sorry, they should NOT be passing by reference. Java is strictly pass-by-value, there is NO pass-by-reference. How does that work? All object variables are pointers to said object.

int i = 5;

i is a variable that holds the value "5"

Integer i = new Integer(5);

i is a variable that holds the memory address of an Integer object that holds the value "5". Let's just say for argument's sake it's 0xBADF00D

when you pass that to a method, it gets passed by value, therefore you cannot assign to it and expect the changes to carry over outside of the method's scope.
void main(String[] args){   Integer i = new Integer(5);   //1.)   setValue(i);   //4.)}void setValue(Integer j){   //2.)   j = new Integer(7);   //3.)}

1.) i is currently pointing to 0xbadf00d, which is occupied by an Integer object holding the value 5.
on the next line, 0xbad00d is copied from i to j because Java is pass-by-value
2.) j is currently pointing to 0xbadf00d, which is occupied by an Integer object holding the value 5.
the next line changes the value of j
3.) j is currently pointing to 0xfa57ca7, which is occupied by an Integer object holding the value 7.
4.) i is currently pointing to 0xbadf00d, which is occupied by an Integer object holding the value 5.

memory location 0xbadf00d still has the Integer object that has the value 5 and
memory location 0xfa57ca7 still has the Integer object that has the value 7

we never changed i, because i never got passed, it got copied.
dalleboy
dalleboy
class MutableInteger{   public MutableInteger(int j)   {      i = j;   }   public int i;}void main(){   MutableInteger k = new MutableInteger(47);   mutate(k);   if (k.i != 47)   {      System.out.WriteLine("Mutated!");   }}void mutate(MutableInteger l){   l.i = 123;}
Steers
Steers
Capn - I'm not quite sure what you are saying, but yes i know the memory address is passed. If you change a variable of an object then it will be changed since objects pass a memory adress not copys. Only primitives pass copys.

dalleboy - You hit it right on the head. The wrapper classes for Java are immutable. I had to make my own mutable Integer class.

What had happened is to change the value i was declaring a new Integer(value) which of course gave a new memory address and i was changing a copy. With a mutable Integer i'm able to change the value without changing the memory address thus, our values have changed and not copys of them.

:D thanks for the help yall, take care.
capn_midnight
capn_midnight
Quote:
Original post by Steers
Capn - I'm not quite sure what you are saying, but yes i know the memory address is passed. If you change a variable of an object then it will be changed since objects pass a memory adress not copys. Only primitives pass copys.

dalleboy - You hit it right on the head. The wrapper classes for Java are immutable. I had to make my own mutable Integer class.

What had happened is to change the value i was declaring a new Integer(value) which of course gave a new memory address and i was changing a copy. With a mutable Integer i'm able to change the value without changing the memory address thus, our values have changed and not copys of them.

:D thanks for the help yall, take care.

that's what I said.
Steers
Steers
Oh well in that case, thanks capn you hit it right on the head too...i think lol

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.