String s1=new String("cow");
String s2=new String("cow");
As per my understanding, if we use ==, the control will check for references but not for objects. But why is that in the above example, s1==s2 is false, eventhough they have the same hashCode? Strings are immutable and when a new string is initialised, the control checks for the existing strings in the string pool and if it matches the existing one with the new one, then the control will assign the new variable to the existing string object. So I should get true when I use s1==s2 since their hashcodes are equal. But I am getting false.
Any explanation on this issue why we are getting false will be appreciated.
Two different objects can have the same hashcode. The hashCode() method is only required to guarantee that two objects that are equal will both return the same hashCode. It does not have to guarantee that two different objects return different hashcodes.
Moreover as per Strings immutability concept, first the control will check in the String pool in VM whether this string is there or not and if it is there, then the reference is pointed to the string obj. So now all the four references are pointing to the same obj(evident from their hashCodes)
I am still not clear with your argument. Can you please provide further explanation to this please....
Originally posted by Paul Sturrock:
Where did you get this definition from?
Probably from the Javadoc for the Object class
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
@OP - the hashCode method is overridden in the String class
The hash code for a String object is computed as
s*31^(n-1) + s*31^(n-2) + ... + s[n-1]
using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation. (The hash value of the empty string is zero.)
I got the hashCode() definition from the following link...
So as per your explanation, hashCode() does not the exact memory address and memory address is involved in generating hashCode().
If we override equals() method, then why should we override the hashCode()?
Even if I don't override hashCode(), I will still get the output that I want from equals() overridden definition when comparing two objects. Is the overridden of hashCode() is done because of definition? and not for anything else(if two objects are equal, then their hashCode() must be equal)Please let me know.
public class equalst
public equalst(int a,String b)
public static void main(String args)
equalst t=new equalst(1,"xx");
equalst t1=new equalst(1,"xx");
public boolean equals(Object t1)
if ((this.a==t.a) && ((this.b).equals(t.b)))
The reason that Hashmaps call the hashCode method is that it is quicker to compare two ints than it is to compare two objects. So, if obj1.hashCode() != obj2.hashCode(), there is no need to call the equals method.