• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

hashCode and == ...

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I am a bit confused about the hashCode method. By default (ie. no overriding), hashCode() returns the memory address.
But since a & b were created using New, wouldn't the memory address be different ? This is easily validated by performing an a==b compare which returns false.
If the memory address is different, why does a.hashCode() and b.hashCode() return the same value ??
Here's the code I used to test ... (the output is just "hashcode equal) ...
------------------------------------------
class Testhashcode {
public static void main (String [] arg) {
String a = new String("ABC");
String b = new String("ABC");
if (a == b) System.out.println("== equal");
if (a.hashCode() == b.hashCode()) System.out.println("hashcode equal");
)
}
[ July 08, 2003: Message edited by: bennido kool kat ]
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The String class overrides equals() to return true if the in parameter is the same sequence of characters as the object.
 
Edwin Keeton
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I should have said that the String class overrides hashCode().
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I am not quite sure if the hashCode() method returns the memory address???
hashCode() method returns an integer such that if 2 objects are equal(determined by the equals method) then their hashCode values are equal.
Whereas when 2 objects are not equal, it is not necessary that their hashCode values need to be distinct. They can be equal.
There is a nice article on the newsletter section which talks about hashCode and equals method.
http://www.javaranch.com/newsletter/Oct2002/newsletteroct2002.jsp#equalandhash
Hope that helps ..
Lalitha
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I am a bit confused about the hashCode method. By default (ie. no overriding), hashCode() returns the memory address.


hashCode() method doesn't returns the memory address but the == operator does that.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "==" method doesnt return true, because if you do a new, then the Thread pool is not utilised to reference an existing object. The Hashcode contract specifies that two identical objects should return the same hashcode. Since the two strings are identical in values, the hashcode produced by them is the same.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic