wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes memory address of object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "memory address of object" Watch "memory address of object" New topic
Author

memory address of object

Raj chiru
Ranch Hand

Joined: Aug 12, 2008
Posts: 141
Hi.......




My dout is which one will represent the memory address of the object whether toString()(Classname@19821f) or hashcode()?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3429
    
  12
raj chiru wrote:My dout is which one will represent the memory address of the object whether toString()(Classname@19821f) or hashcode()?


Neither. The Javadoc for the Object class explains what both those vales represent.


Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
The two numbers are the same, if you convert them from hexadecimal to decimal.

The correct answer to your question is "don't know." When you are using a high-level language you avoid even knowing the memory addresses of anything. If you look at the Object class you get a hint, but it is only a hint and not definitive.

Remember that garbage collection may move the memory location of an object, so the hint given may give inaccurate information. You can get similar information with System#identityHashCode(java.lang.Object).
Raj chiru
Ranch Hand

Joined: Aug 12, 2008
Posts: 141
Hi...campbell
Thanks for your reply.
But i have one more dout i.e java is assign a hashcode based on the object's memory address on the heap, so what is need to generate the hashcode? and why can't you use the memory address value as a unique ID for object in place of hashcode value?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18538
    
  40

raj chiru wrote:But i have one more dout i.e java is assign a hashcode based on the object's memory address on the heap, so what is need to generate the hashcode? and why can't you use the memory address value as a unique ID for object in place of hashcode value?


Because the GC moves objects. And if the hashcode changes, when the object is used as a key in a hashing collection, it will corrupt the collection.

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Raj chiru
Ranch Hand

Joined: Aug 12, 2008
Posts: 141
Hi....Henry,
Thaks for your reply,
Is there any way to find out memory address of the object?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

No. The JVM hides that information from you. The identity hash code (as returned by Object.hashCode and System.identityHashCode) may or may not be an indication, but you can never be sure. A different JVM implementation, or even a different JVM version, may change the hash code implementation to return something completely different.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

raj chiru wrote:Is there any way to find out memory address of the object?

Even if you could, it would be meaningless. As someone has pointed out, the GC can move objects around in memory.

How would you handle this:

int memAddress = <some theoretical call to get memory address>;
doSomething(memAddress);

and unbeknownst to you, the GC runs in between those two lines of code, and moves stuff around. Now, you have your memAddress NOT POINTING AT THE OBJECT you think it's pointing to. In fact, it could be pointing to a different object entirely.

Further, what would you DO with the address? There is no pointer arithmetic (ala C/C++), you can't explicitly free it, copy into or from it... Java simply doesn't support that.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
 
subject: memory address of object
 
Similar Threads
toString method
about toString method
HashCode and Equals
Output of a simple code
Return Values