Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

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.
 
I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic