File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Doubt on  String Comparisson Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt on  String Comparisson" Watch "Doubt on  String Comparisson" New topic
Author

Doubt on String Comparisson

vijay sachin
Greenhorn

Joined: Jan 18, 2011
Posts: 23
Hi All,

I have a doubt on string Comparison, let me explain


HashCode is same for all the above three objects

My Doubts are

1) What is Hashcode? (I searched in internet I found: Hashcode is a unique Id given to the Object
If it is unique then in the above all the three objects as same Hashcode how is possible?
2) Can some body explain how JVM will create Hashcode for object?
3) If I do (s2 ==s3) it will give as NOT EQUAL. but if I do (s2 == s1) it will give Both are Same.
What is the reason behind this?
4) What is String Constant pool in java? (All the String Object will be stored here ?)

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11229
    
  16

1) A 'Hashcode' is basically a number you calculate. The idea is that you use a formula that will give you distinct values for objects that are 'different', according to your definition of 'different'. So for strings, you would most likely use the content of the string somewhere in your calculation so that "hello" would return a different value than "goodbye".

1a) Note that you only have TWO objects above. s1 and s2 both point to the same instance of "hello" in the string pool.

2) I believe the Object class has a hashcode() method defined. It doesn't do much, and is not terribly useful. Many subclasses will have the method redefined. If you define your own class, you will often need to define your own hashcode method.

3) s1 and s2 both point to the same object in the string pool. But since s3 is set to a "new String("hello")", the new operator creates a new object, NOT in the string pool, with the same value. So, since the '==' operator tells you if you are referring to the same object, and since s2/s3 do NOT, it returns FALSE. since s1 and s2 DO both point to the same object, it returns TRUE.

4) The string pool is just that. All string literals in your code (anything inside double-quotes) get build in the string pool. Strings are the only things that work this way (as far as I know). So, s1 and s2 both point to the same object.

since you use the word "new" on line 3, the String object in the string pool with a value of "hello" is used as a sort of seed to build a new object, that happens to be a string. This string is NOT in the string pool.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

vijay sachin wrote:... What is Hashcode? (I searched in internet I found: Hashcode is a unique Id given to the Object...

The internet is wrong. Hashcodes are not necessarily unique.

The API documentation for Object's hashCode() method should answer most of your questions.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Also, see Strings Literally.
kumarjit banerjee
Ranch Hand

Joined: Mar 27, 2011
Posts: 32

marc weber wrote:
vijay sachin wrote:... What is Hashcode? (I searched in internet I found: Hashcode is a unique Id given to the Object...

The internet is wrong. Hashcodes are not necessarily unique.

The API documentation for Object's hashCode() method should answer most of your questions.






The hashCode() method is defined to provide an unique value to an object. But since it returns an int it is clear that the maximum number of unique hashCode() for all objects is only 2^32. In the above example the output for both SOP is 347. The hashCode() method of the class String is built upon the logic of polynomials. In this case the polynomial has n number of coefficients where n is the number of characters in the String object.
Now, 11*31+6=347.
That is why the hashCode() is same for them.

The == operator compares the memory locations of the objects. So when creating a String with new operator creates a new String object in the memory. For String literals, jvm first checks whether there exists any String literal in the String pool. If one exists then the reference is returned and if not a new literal is created. A String literal once created resides in the memory until the garbage collector runs even when all the references to the literal is no longer pointing to it. It is internal to the jvm. This is the reason why the password is read in an array of character rather than String (java.io.Console).

However the String class provides a native method "intern". It works like this



The above code will return true.




marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

kumarjit banerjee wrote:... The hashCode() method is defined to provide an unique value to an object...

Be careful with this wording, because this is a common misperception. A hash code is not defined to be unique. Note the first 6 words of the API documentation for Object's hashCode method...
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects.

As expanded on by Wikipedia...
A hash function may map two or more keys to the same hash value. In many applications, it is desirable to minimize the occurrence of such collisions, which means that the hash function must map the keys to the hash values as evenly as possible.

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11229
    
  16

There could be some confusion here. Hashcodes ARE unique in that for a specific instance of a specific object, the Hashcode value will always be the same thing. in other words, an object will have one and only one hashcode value.

However, a given hashcode value CAN have MANY object map to it. So in that aspect, they are NOT unique. I find this statement to be ambiguous:

"The hashCode() method is defined to provide an unique value to an object."

the object has a unique value, but the value does not (necessarily) have a unique object. object to hashcode values are many to one.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt on String Comparisson