Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

another reference question  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 517
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok this will be hard to explain but I will do my best

as you can see in my code I am creating a reference temp then setting it = to a body I add that body to my set and map then I repeat this process for every body I want to add to them

anyway as I know when we add objects to a list,set,map a reference to that object is added or a copy of that reference I should say not the actual object anyway so how can I keep using temp in this way if temp keeps changing?

wouldn't when I'm printing out my set of planets wouldn't the reference body be printed out and body equals Jupiter??

so confused could someone try explain this to me maybe with an example or even using my example that would be great

much appreciated

thanks guys I'll post the code below

 
Adam Chalkley
Ranch Hand
Posts: 517
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Adam Chalkley
Ranch Hand
Posts: 517
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Zachary Griggs
Ranch Hand
Posts: 83
10
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if I understand the question, you're asking why this line does not overwrite everything else in the Map that was added before:


The answer is because of how java passes arguments.
In your java lessons, you may learn that, "natives are passed by value, objects are passed by reference". This is incorrect, everything in Java is passed by value. It's just that there is no such thing as an object value, only object references.
With this in mind, consider what the program does, line by line.


This line allocates an object reference, of type Body. It also assigns the object reference to point to a Body object in memory (name is "Earth", orbital length is 356).

Here, the following things happen:
1) A method call to getName() is made. This returns a reference to the String with the contents "Earth". Due to String internal details, this may or may not be the same reference as used in other areas of the code, but this is unimportant.
2) A copy of the reference temp is made. Both these references point to the same object in memory, but they are not the same.
3) The put method then stores the copied object reference


This performs a method call to Set.put. This is where by value passing really matters. The following happens:

1) A copy of the reference temp is made. Both these references point to the same object in memory, but they are not the same.
2) This copy of the reference is passed to the put method
3) The add method then stores the copied object reference

Now, we re-assign temp.

This changes where the reference "temp" points. It no longer points to the Earth object, but a new object for Mars.
But remember, when we stored temp in the map, we did not store temp's reference at all but a copy of temp's reference. Therefore the thing we put into the map is not changed; it points to the old Earth object. We can go on like this and reassign references to new objects as we wish, and we are guaranteed none of our previous method calls will be affected. Note that this is NOT the same as calling methods that change the actual object that is pointed to. If we do that, then it will affect the data that is stored in those data structures because we're now changing the object itself, rather than just the reference.

Also, on a sidenote: consider refactoring your code. For example these 3 copy paste lines are just asking to be put into a method.

Something like this:
 
Adam Chalkley
Ranch Hand
Posts: 517
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Zachary for the reply much appreciated

I don't understand this code too well,

we are calling the addMoon method on temp so we are adding a moon to a set for temp I understand that but then we assign temp to point at another object which is now Jupitur rather than Saturn we then add a moon to Jupiter but how does this moon get added because we are changing where temp is pointing too so even though we are added a moon to  Saturn,temp is now Jupiter so how can we access saturns moon if temp points to Jupiter

just say we have a method in the Body class getMoon??

thanks


 
Knute Snortum
Sheriff
Posts: 4189
122
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) temp refers to Saturn object
2) planets.add(temp) adds a copy of that reference
3) temp changes to refer to Jupiter
4) this does not affect planets because it has a copy of the reference already
 
Adam Chalkley
Ranch Hand
Posts: 517
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the reply Knute

but when we say temp.addMoon(tempMoon);

what happens here because temp refers to saturn then we add temp(saturn) to the set we then add a moon to temps set moons,we then reassign temp to point to jupiter and we add it to planets set then add two moons,

so the confusing part for me is when we are printing out the moons for each planet

temp refers to Jupiter at this stage so when we execute this code what is happening?



we are saying moons.addAll(moon.getMoons()) I probably should have changed moon to planet to ease confusion but anyway the first time the loop runs moon equals Saturn(in our case with just two planets) we then add all it's moons to the new set but the confusing thing is how does Saturn even have any moons? because we added temp to the set of planets BEFORE we added any moons to temp which in the first case was Saturn?

I'm not great at explaining thanks
 
Knute Snortum
Sheriff
Posts: 4189
122
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...because we added temp to the set of planets...

No, you added a copy of what temp refers to.  I think your confusion comes from thinking that you add variables to the Set.
 
Zachary Griggs
Ranch Hand
Posts: 83
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how does Saturn even have any moons?

Saturn has moons because of this code here:

In memory:
1) The object reference "temp" is made to point to a new Body object in memory, with name "Saturn"
2) Adds a copy of the object reference to solarSystem and planets
3) The object reference "tempMoon" is made to point to a new Body object in memory with name "moon1"
4) Adds a copy of the tempMoon reference to solarSysem (which points to a Map)
5) Adds a copy of the tempMoon reference to temp (which points to the Saturn object)

Now in memory we have:
- A Body object named "Saturn" with orbital length 120.
- This Body stores a reference to another Body. This other one has name "moon1" and orbital length 88

However much you re-assign temp and tempMoon, those reference copies won't change. This Saturn object will remain unchanged, pointing to moon1.

because we added temp to the set of planets BEFORE we added any moons to temp which in the first case was Saturn?

It doesn't matter what order you do it in here, because it's an object. Remember, "temp" is a reference, not an object. If you add a copy of that reference to a Set (points to the same object), and then edit that object using any of the references pointing to it, it will change everywhere because there is really only one object there, we just refer to it in many different ways.
 
Adam Chalkley
Ranch Hand
Posts: 517
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Knute =)

and thanks Zachary for the detailed explanation =)

makes sense now
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!