-1
String s1 = new String("abc");//it is created at heap area
String s2 = "abc";//it is created string constant pool 
System.out.println(s1==s2);//false
System.out.println(s1.hashCode());same hashCode
System.out.println(s2.hashCode());same hashCode

The last two lines give the same hashCode(), but the objects are different.while creating s1 object it create in heap area and s2 object is create in string constant pool.but both are gives same hashCode.i.e my doubt?

5 Answers5

3

The String javadoc states

Returns a hash code for this string. The hash code for a String object is computed as

s[0]*31^(n-1) + s[1]*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.)

Since the String objects have the same characters, their hashcodes are equal.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
2

Because String#hashCode() is based on the character contents, not object identity:

/**
 * Returns a hash code for this string. The hash code for a
 * <code>String</code> object is computed as
 * <blockquote><pre>
 * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
 * </pre></blockquote>
 * using <code>int</code> arithmetic, where <code>s[i]</code> is the
 * <i>i</i>th character of the string, <code>n</code> is the length of
 * the string, and <code>^</code> indicates exponentiation.
 * (The hash value of the empty string is zero.)
 *
 * @return  a hash code value for this object.
 */
public int hashCode() {
    int h = hash;
    if (h == 0 && value.length > 0) {
        char val[] = value;

        for (int i = 0; i < value.length; i++) {
            h = 31 * h + val[i];
        }
        hash = h;
    }
    return h;
}
maksimov
  • 5,792
  • 1
  • 30
  • 38
0

Simplest answer: by definition, when two objects are equal -- defined here as having the same contents -- they are supposed to have the same hashCode. This is called the contract for these methods. Classes that break the contract won't work as keys in a HashMap , for example, and are otherwise generally broken.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
0

This line:

String s1 = new String("abc");

creates a new String object. This line:

String s2 = "abc";

creates a String object that is interned to the String pool (all uses of the String constant "abc" will use that same object from the pool).

== compares if the two operands are the same object, so s1 == s2 is false as expected.


Hash codes are not required to be different for different objects. Hash codes are required to be the same if they have the same value - ie if a.equals(b). If the two string objects have the same value, then they will have the same hash code: The hash codes are the same, as expected.

maksimov
  • 5,792
  • 1
  • 30
  • 38
Bohemian
  • 412,405
  • 93
  • 575
  • 722
-1

The “==” operator compares the objects’ location(s) in memory

I guess you should read the difference between ".equals" and "==". Please refer the following link

http://www.programmerinterview.com/index.php/java-questions/java-whats-the-difference-between-equals-and/

aviundefined
  • 802
  • 2
  • 10
  • 25