File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A silly question but essential

 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
What is yhe difference between reference and object
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm moving this to Java in General (Beginner).
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic