wood burning stoves*
The moose likes Beginning Java and the fly likes A silly question but essential Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A silly question but essential" Watch "A silly question but essential" New topic
Author

A silly question but essential

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
What is yhe difference between reference and object
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I'm moving this to Java in General (Beginner).


"I'm not back." - Bill Harding, Twister
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
A reference is also sometimes called a handle, and it's much like a pointer in C - it's a relatively small data structure that points to a particular object instance (which itself may take up quite a bit more memory than the reference does), telling the JVM where it can be found in memory. Objects are (almost?) always manipulated through references, and one object can mave multiple references which refer to it, e.g.:
<code><pre> String a = new String("object");
String b = a;
String c = a;</pre></code>
There are now three references, a, b, and c, all of which refer to the same String object "object".
Most of the time when you see a reference in code, its use ends up invoking or affecting the actual object which it references. A notable exception to this however is the assignment operator - when a reference appears on the left hand side of an = sign, whatever object the reference may have previously referred to is completely unaffected and irrelevant - only the reference itself is affected by the assignment satement. So:
<code><pre> String a = new String("object");
String b = a;
a = new String("other");</pre></code>
On the third line, reference a is reassigned to the new String "other" - but this has no effect on the (previously-)underlying object "object", and the reference b still refers to "object". (I.e. it hasn't also been replaced with "other".)
Here I'm just babbling about what I think are the most commonly misunderstood issues regarding references vs. objects. If I addressed your concerns, great; if not, please post again with a more specific question.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A silly question but essential