File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Assigning References Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Assigning References" Watch "Assigning References" New topic
Author

Assigning References

Rakesh Mehra
Ranch Hand

Joined: Nov 20, 2007
Posts: 31
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

Joined: Jul 11, 2001
Posts: 14112
And your question is...?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Rakesh Mehra
Ranch Hand

Joined: Nov 20, 2007
Posts: 31
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

Joined: Nov 20, 2007
Posts: 31
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

Joined: Aug 16, 2005
Posts: 13875
    
  10

Have a look at the following JavaRanch Campfile stories:

Cup Size -- a story about variables
Pass-by-Value Please (Cup Size continued)


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assigning References
 
Similar Threads
Where's the old object?
some prob with ...comparing object..
Assigning references
equals() fails here
problem from khalid's book