• 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

Problem on String

 
Greenhorn
Posts: 26
Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I am preparing for SCJP 6 and i have one Question on String see the code below


Output:
hash code for str1: -1704812257
hash code for str2: -1704812257

str1.equals(str2): true
str1 == str2: false

str1 and str2 are pointing to only one reference(same hashcode), so str1 == str2 should be true

Thank you all
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

str1 and str2 are pointing to only one reference(same hashcode), so str1 == str2 should be true



Hashcodes have nothing to do with there references. With the Object class, they might have been originally generated from the references. With other classes, such as String, Integer, Short, Byte, etc., they have nothing to do with their reference locations.

Henry
 
Srikanth Nakka
Greenhorn
Posts: 26
Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks for your help and i have one more doubt that

Hashcodes have nothing to do with there references


so what is the use of hashcode, if you can specify an example that better .

Thanks
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Srikanth Nakka wrote:
Thanks for your help and i have one more doubt that

Hashcodes have nothing to do with there references


so what is the use of hashcode, if you can specify an example that better .

Thanks



Hashcodes are use by the hashing collections -- Hashtable, HashMap, HashSet, ConcurrentHashMap, etc... See those classes for more information.

Furthermore, the subject of hashing is not specific to Java. You may want to google hashing for more information on the topic.

Henry
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hash code contract states that:
"
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.
"

In your example, strings are equal as per the equals() method and therefore they must return same hascode value from the hashCode() method.

Hopefully, this makes sense. Let me know if you have any questions related to what i said.
 
Jyoti B.Shah
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just adding little more:

Objects are considered equal if they are equal as per the equals() method and not "==" operator. Equals() method(and not ==) is used by hashCode() to determine the hascode for an object.

Also, As per the contract, it is not required for two unequal objects to have different hashcode. In a bad implementation of hascode, you can have same hash code value for two different objects(their euals() method returns false). It is perfectly legal.
 
Villains always have antidotes. They're funny that way. Here's an antidote disguised as a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic