0

why hashcode value is same?

public static void main(String args[])
{
    String s1="abc";
    String s2=new String("abc");
    System.out.println("Hashcode s1-:"+ s1.hashCode());
    System.out.println("Hashcode s2-:"+ s2.hashCode());
    if(s1==s2){
        System.out.println("==true:");
    } 
}

output

Hashcode s1-:96354
Hashcode s2-:96354
Sylvain Defresne
  • 42,429
  • 12
  • 75
  • 85

9 Answers9

7

The hash code for two equal objects should be equal.

In this case, the objects are strings and they are considered equal because they hold the same sequence of characters, "abc".

If you want a hash code that is based on object identity rather than equality use System.identityHashCode().

Joni
  • 108,737
  • 14
  • 143
  • 193
  • 1
    I think his confusion lies with the fact that his test for `==` returns false. – DaveJohnston Mar 18 '13 at 09:31
  • Thanks for your valuable answer – Mahesh Pachpande Mar 18 '13 at 09:36
  • In above example I am creating a object with a new keyword hence it should be created on heap and other would crate on string pool though it gives same hashcode values – Mahesh Pachpande Mar 18 '13 at 09:43
  • Yes, that is what happens. Do you have problem with that? Or further questions? – Joni Mar 18 '13 at 09:50
  • @MaheshPachpande using `==` to compare objects only compares the references, i.e. do they point to the same object on the heap? When comparing Strings for equality you should use the `equals` method, i.e. `if (s1.equals(s2))` this will compare the actual value of the string. – DaveJohnston Mar 18 '13 at 10:30
  • The specification for `hashCode()` says that if `o1.equals(o2)`, then their hash codes must be equal. `hashCode()` is specified in terms of `equals`, not `==`. – Louis Wasserman Mar 18 '13 at 17:12
3

Why wouldn't they be the same? The hashcode is computed on the contents of the string, hence they are the same for both.

== compares object references, and because you used new String for s2 the references are not the same.

You should be using the equals method to test equality of strings based on their value.

DaveJohnston
  • 10,031
  • 10
  • 54
  • 83
2

Because the hashcode is computed using the formula which takes in only the characters present in the string. Same characters in a String will yield the same hashcode.

Javadoc for the computation formula.

Rahul
  • 44,383
  • 11
  • 84
  • 103
0

This is because of how the hashcode of String are computed in java.

Check the javadoc : http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

And more over, your two strings are equals, therefore their hashcode must be the same.

benzonico
  • 10,635
  • 5
  • 42
  • 50
0

This is the code...therefore the result is the same for two equals objects:

public int hashCode() {
int h = hash;
    int len = count;
if (h == 0 && len > 0) {
    int off = offset;
    char val[] = value;

        for (int i = 0; i < len; i++) {
            h = 31*h + val[off++];
        }
        hash = h;
    }
    return h;
}
sk2212
  • 1,688
  • 4
  • 23
  • 43
0

as per the rules, objects for those equals method return true, should have the same hashcode.

Ankit
  • 6,554
  • 6
  • 49
  • 71
0

Hashcode for a string is computed based on its characters and so is equals()

It is calculated as

s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]

where s[i] is each character for 0<=i<n and n is its length.

Both your strings have the same content and thus hashcode is the same.

Naveed S
  • 5,106
  • 4
  • 34
  • 52
0

String class has its own hashcode method implemeted in it. Its method will compute the hashcode as: s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]

So for the same character sequence hashcode will be same.

G.S
  • 10,413
  • 7
  • 36
  • 52
0

The JVM doesn't create a new String if this already exist, it just returns the reference. A new String will be created when you try to change the actual String in one of the vars. You can check it debugging the application, the String objects will have different memory addresses but the value inside will have exactly the same memory address.

fiso
  • 1,375
  • 1
  • 14
  • 20