Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

dereference

 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
fred rosenberger
lowercase baba
Bartender
Posts: 12185
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic