• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in hashCode()method of String

 
Gajendra Tomer
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

This is regarding String Pool & hashCode() method of String class.

My understanding:

For identical String values JVM doesn't create new Object in memory. It reassigns reference of existing identical String value to new

Objects. Therefore, 2 Objects with similar String values shall always carry equal hashCode() & memory address.

Hence "==" operator should return "true" for such 2 String Objects as they are referring same memory reference.

Program Definition:




OUTPUT: false



Query: At Point-(1) & (2) above same hashCodes are displayed,by that way it should not give "false" as output. I am expecting "true".


Answer: ???


Thanks & Regards,
Gajendra





 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gajendra instead of

String s1=new String("Gajendra");
String s2=new String("Gajendra");

Try this

String s1= "Gajendra";
String s2= "Gajendra";
 
Djonatah Stiegler
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit is right...

When creating string objects like this:


You are creating two string objects, one in the string pool and another object on the heap.

So, on your code, you are trying to compare two different objects.
<><
 
Gajendra Tomer
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks for your responses but motive is not to get "true" in comparison of s1 & s2.

Why hashCodes() are identical if references are different?


Regards,
Gajendra
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well did you see the documentation for the hashCode method of the String class. It doesn't depend on the memory address of the object. So hashCode of String class would return same hashCode for same same values...
 
Dhruv Goel
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you create a string by

String s=new String("scjp");

then a new object is created on the heap and which is not refering to the same string in the pool.....

Now hashcode is coming out to be same because hashcodes give equal results when s1.equals(s2)==true and not when s1==s2.

 
Gajendra Tomer
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks All
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well did you see the documentation for the hashCode method of the String class. It doesn't depend on the memory address of the object. So hashCode of String class would return same hashCode for same same values...



This behaviour is exhibited by the String Class alone???
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are many more classes which override the hashCode method like Wrapper classes. You have to look at the API to get full list of those classes.
 
Henry Wong
author
Marshal
Pie
Posts: 20882
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This behaviour is exhibited by the String Class alone???


The string class' hashcode method has been overridden to return a value based on the string itself. Hence, if two strings are equal() based on the equals() method, there hashcodes are equal as well. (This is also required as part of the equals/hashCode contract).

Henry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic