Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

dereferencing or referencing?  RSS feed

 
Daniel .J.Hyslop
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am still a little confused over the term dereferencing. I know when we reference an object we use a variable to acheive this , the said variable then references an object.What actually is meant and is physically happening when we dereference an object. I read a thread further down the list on this page named "null" .There was an answer to the problem in question where a method which returned null was referenced by another method which returned an int ie int i = method1().method2(); , what was meant by dereferencing the method
thanks danny
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you say

Foo myFoo = new Foo();

myFoo does not hold the actual object. if you could look at the bits and bytes, all that's in myFoo is a couple of bytes that represent where in memory the real object is - the memory address. so we say that myFoo holds a REFERENCE to the actual object.

when you use the variable myFoo to call a method, something like

myFoo.printInfo();

the JVM automatically "dereferences" the variable to find the real data. You don't have to worry about it.

This is different than a language like C or C++. you could actually change where a variable referred to in memory. And you had to explicitly tell the compiler to "de-reference" the variable to get the actual object, instead of the memory address.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Daniel .j.Hyslop:
...What actually is meant and is physically happening when we dereference an object....

As fred's example above illustrates, you don't dereference an object; you dereference the variable to get to the object.

Layne
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!