• Post Reply Bookmark Topic Watch Topic
  • New Topic

Address vs reference  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we find the address of an reference variable in Java
 
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please take time to Carefully Choose One Forum when you start a topic. This question has nothing to do with Struts. I'll move it to a more appropriate forum...
 
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean the memory address?

The short answer is no.

First of all, references can't be accessed in the way pointers can. There's nothing you can do with them except dereference them.

I take it you want to get the address of an object. Even so, you can't get their memory address, and you shouldn't either.

You *could* get the address of an object in heap space, I think, by calling the objects hashCode() method. However, this only works for objects that don't have their hashCode() method overridden. Secondly, it is not guaranteed that the JVM will return the memory address, this is simply the typical implementation.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:You *could* get the address of an object in heap space, I think, by calling the objects hashCode() method. However, this only works for objects that don't have their hashCode() method overridden. Secondly, it is not guaranteed that the JVM will return the memory address, this is simply the typical implementation.
You can use System.identityHashCode(Object x) to obtain the hashCode for an Objec of a class that has overriden the method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!