Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assigning References

 
Rakesh Mehra
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Request help in uderstanding the below highlighted

Copying references by assignment creates aliases. The following example recapitulates that discussion:

Pizza pizza1 = new Pizza("Hot&Spicy");
Pizza pizza2 = new Pizza("Sweet&Sour");

pizza2 = pizza1;

Variable pizza1 is a reference to a pizza that is hot and spicy, and pizza2 is a reference to a pizza which is sweet and sour. Assigning pizza1 to pizza2 means that pizza2 now references the same pizza as pizza1, that is, the hot and spicy one. After assignment these variables are aliases, and either one can be used to manipulate the hot and spicy Pizza object.

Assigning a reference does not create a copy of the source object denoted by the reference variable on the right-hand side. Reference assignment also does not copy the state of the source object to any object denoted by the reference variable on the left-hand side. It merely assigns the reference value to the variable on the right-hand side to the variable on the left-hand side, so that they denote the same object.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And your question is...?
 
Rakesh Mehra
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
And your question is...?


I'm not able to understand the below:

Assigning a reference does not create a copy of the source object denoted by the reference variable on the right-hand side. Reference assignment also does not copy the state of the source object to any object denoted by the reference variable on the left-hand side. It merely assigns the reference value to the variable on the right-hand side to the variable on the left-hand side, so that they denote the same object.
 
Rakesh Mehra
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
And your question is...?


Help me in understanding the below:
Assigning a reference does not create a copy of the source object denoted by the reference variable on the right-hand side. Reference assignment also does not copy the state of the source object to any object denoted by the reference variable on the left-hand side. It merely assigns the reference value to the variable on the right-hand side to the variable on the left-hand side, so that they denote the same object.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15485
43
Android IntelliJ IDE Java Scala Spring
 
Campbell Ritchie
Sheriff
Pie
Posts: 50240
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I can understand your problem. You started off with thisThen you assigned the pizza2 reference to the hot and spicy pizza. Now you have
The sweet and sour is no longer referenced by anything, and vanishes into some sort of cyber-limbo until either your JVM exits or a garbage collector finds and deletes it.

[tangent]Round here we have foxes which seem to live on dropped pizza. I don't know whether they prefer spicy pizza or sweet.[/tangent]

What the bit about "not create a copy" means is:

You have one hot and spicy pizza with two names. You do not have two hot and spicy pizzas.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic