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 ?)
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.
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.
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.
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.
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.