permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in hashCode()method of String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in hashCode()method of String" Watch "Doubt in hashCode()method of String" New topic
Author

Doubt in hashCode()method of String

Gajendra Tomer
Ranch Hand

Joined: Sep 22, 2008
Posts: 32
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

Joined: Aug 03, 2008
Posts: 9280
    
  17

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: 32

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

Joined: Aug 03, 2008
Posts: 9280
    
  17

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: 32

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: 18108
    
  39

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)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in hashCode()method of String
 
Similar Threads
scjp ( == vs .equals() )
I am Confused !!!!! Pls Help
Question on Strings and String pool
please explain the output?
"==" in String Class and Wrapper Class