• 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

Java HashCode Problem

 
Ranch Hand
Posts: 87
jQuery
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Please any one clarrify my doubt . That is how == method work on Objects references. is if two object have same hashcode then i say that two objects are same .

Consider the follwing things



From the above code , s1 and s2 have same hash code but when i use s1==s2 result is false

Why this happening.

 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very simply put, because they are two distinct instances of String - two completely seperate Objects in the heap.
When you use the == operator to compare two Objects, you are comparing them by their reference.
In this case the two references point to different Objects, so the result is false.
An Object's hashCode() value is not the same as an Object's reference - it is caluculated in a way specific to the Object's class.
The String class caluculates the hashCode() value based on the internal char array, which is identical for both String instances.
The hashCode() values are therefore identical, even if the Object references are not.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S Subbu wrote:Please any one clarrify my doubt . That is how == method work on Objects references. is if two object have same hashcode then i say that two objects are same.


No, that's not true. It's entirely possible to have two different Objects with the same hash code. hashCode() returns an int. Think about it - how many different int values are there? It's not hard to have more objects than that.

== returns true if and only if the references are to exactly the same Object.
 
S Subbu
Ranch Hand
Posts: 87
jQuery
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks Jelle Klap ,Matthew Brown

If two references points same object then == is return true. is it correct , hashcode does not concern for == comparision.

My assumption is if two references hashcodes are equal if and only if two reference points same object .

My assumption is true rather than strings ?




 
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

S Subbu wrote:My assumption is if two references hashcodes are equal if and only if two reference points same object.


That's the bit that's not true. It is definitely possible to have two different objects with the same hash code.
 
S Subbu
Ranch Hand
Posts: 87
jQuery
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Matthew Brown , Now i got the point . But i have another doubt

1.what is the need of the hashcode?

2.How java checks two references points same objects?



 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. The purpose of the hashcode is to assist in putting objects into hash-based collections (i.e. Maps).

2. Java checks whether two references point at the same object by comparing the references to see if they contain the same value.

(By the way #2 is pretty much like asking how Java checks whether two integer values are the same. And also by the way, asking how Java does something is basically the wrong way to learn things. You should learn what it does.)
 
S Subbu
Ranch Hand
Posts: 87
jQuery
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul.

Now i got some clarity.Thanks to all.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic