This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Darcy DeClute's Scrum Master Certification Guide: The Definitive Resource for Passing the CSM and PSM Exams and have Darcy DeClute on-line!
See this thread for details.
  • 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

why hashcode same for String not for Class Object?

 
Ranch Hand
Posts: 247
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

here I am having two String Obect . "s1" is not pointing to the address that "s2" is refering to and vice versa.
I know that hashcode gives the memory address of the object.
then why it gives same hashcode for object "s1" and "s2" and not for class(ArrayMap) object ?
please let me know the real difference.

following is code snippet.




thanks
Raza!
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The default hashCode() method gives the memory address. But plenty of classes override hashCode(). In particular, any class that overrides equals() should also override hashCode() to be consistent (if two objects are 'equal', they should have the same hash code, otherwise it will break the algorithms used in classes like HashMap).

String is a good example of this. String has overridden equals() so that two String objects are equal if they have the same string value. So it also overrides hashCode() as well.

Look at the documentation of hashCode() in the Object class for full details about the contract hashCode() ought to follow.
 
Raza Mohd
Ranch Hand
Posts: 247
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so why hashcode in String Class is overridden in such a way ..
that it gives same integer value for two different objects.

or I think.
if objects are equal (depending upon the way the equals is overridden in a particular class here , String)
then hashCode will be implemented as it should give the same integer value.

am i right?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's right. The documentation says:

If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.



So if you ever override equals, you should override hashCode as well.
 
Yes, my master! Here is the tiny ad you asked for:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic