| Author |
Doubt in hashCode()method of String
|
Gajendra Tomer
Ranch Hand
Joined: Sep 22, 2008
Posts: 31
|
|
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
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
Gajendra instead of
String s1=new String("Gajendra");
String s2=new String("Gajendra");
Try this
String s1= "Gajendra";
String s2= "Gajendra";
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Djonatah Stiegler
Ranch Hand
Joined: Oct 30, 2008
Posts: 32
|
|
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.
<><
|
From Brazil
|
 |
Gajendra Tomer
Ranch Hand
Joined: Sep 22, 2008
Posts: 31
|
|
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
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
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
Joined: Jan 08, 2009
Posts: 9
|
|
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.
|
scjp 1.5 ------> 100%
|
 |
Gajendra Tomer
Ranch Hand
Joined: Sep 22, 2008
Posts: 31
|
|
Thanks All
|
 |
Abhi vijay
Ranch Hand
Joined: Sep 16, 2008
Posts: 509
|
|
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
Joined: Aug 18, 2008
Posts: 598
|
|
|
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.
|
My Blog SCJP 5 SCWCD 5
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
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
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
 |
|
|
subject: Doubt in hashCode()method of String
|
|
|