RatiKanta pal wrote:hi Tsang,
Correct me if i am wrong.hashCode give me exact address in equals methos.out side equals method what hashCode return.How do i get exact physical address of an object.
Short answer: you don't. But then you never need to.
The default hashCode() in java.lang.Object returns a value that is usually calculated using the physical address. But it's not actually the physical address (it can't be: it's only an int and that isn't big enough to describe the address space on a modern machine). Specifically, the documentation says:
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
But hashCode() needs to be consistent with equals(), so any class that overrides equals() should also override hashCode(). And the String class certainly does that - any two String objects containing the same value will have the same hash code.
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop