aspose file tools*
The moose likes Beginning Java and the fly likes dereference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "dereference" Watch "dereference" New topic
Author

dereference

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
in this book i'm reading, author comments this line of code:

saying:
(...)Java dereferences the variable to obtain the String object at which it points and uses this for the comparison


My Q:
what's dereferencing?

TiA


java amateur
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11308
    
  16

In java, varables don't hold the actual object, but a reference to the object. they're like a piece of paper saying "the car is in the garage at THIS address". the paper tells you where the car is, but it is not the actual car.

so, studentname holds where the String object is in memory. by 'dereferencing', they basically mean it goes to the garage to see what kind of car is really in there... or what the actual string is.

In C, you could actually have a variable that was a pointer to somewhere else in memory. you had to do the de-referencing yourself (with the dereference operator). Java does all this automatically.
[ May 24, 2005: Message edited by: fred rosenberger ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
thanks for your answer, fred

i did get what you said, the problem seems to be in what it literally sounds to me

in some google i made, i found out that making something like

obj dot someMethod
actually dereferences obj var...

just sounds weird - in my mind dereference would be like making
obj = null;

(well, maybe because i'm not used to C talk)
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
Originally posted by miguel lisboa:
[QB]
just sounds weird - in my mind dereference would be like making
obj = null;
[QB]

That is what dereferencing does, garbage collection couldn't function if every object created was referenced until the program exited. Think of the memory leakage. In C++ you must explicitly dereference objects but in Java once an object has finished performing its tasks it is automatically dereferenced and available for garbage collection. This is one of the reasons Java has become so popular because it simplifies object creation and destruction
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
after all author does gives a definition:

dereference
to navigate to a memory location based on the memory address stored in a reference
(in Agile Java, by J Langr)
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

Originally posted by Nigel Browne:

That is what dereferencing does, garbage collection couldn't function if every object created was referenced until the program exited.


Not to be particular (Ok, I am being particular) but this is not dereferencing as explained above. This is more releasing the reference; dereferencing is as has been previously described: following the reference to get the actual object.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: dereference