functioning of (== ) in terms of hashCode

75 views Asked by At

String s1="abc";

String s2=new String("abc");

when we compare the both

s1==s2; it return false

and when we compare it with s1.hashCode()==s2.hashCode it return true

i know (==) checks reference id's .Does it returning true in above comparison because above hashCode are saved to same bucket??Please give me explanation

4

There are 4 answers

2
Brian Agnew On BEST ANSWER

Don't forget that your hash codes are primitive integers, and comparing primitives using == will compare their values, not their references (since primitives don't have references)

Hence two strings having the same contents will yield the same hash code, and a comparison via == is perfectly valid.

The concept of a bucket is only valid when you put an object into a hashed collection (e.g. a HashSet). The value of the hash code dictates which bucket the object goes into. The hash codes themselves aren't stored.

0
Rob Johnston On

First comparison fails because they are two different objects. Second comparison works because they are comparing the output of the hashCode() function, which produces the same value for both.

0
Brett Walker On

When comparing objects using the equality operator (==) you are testing the equality of the Object Reference. Object References are only equal if they are the same object.

What is usually meant is the equality of the concept/information the object encapsulates. And this is best determined by the equal(Object obj) method.

For String this would be:

s1.equals(s2);
3
mastov On

When you compare the two Strings using s1==s2, you are comparing references. Since just before that line you created a new String object by copying the old one, both references refer to different objects, even though they contain the same value.

On the other hand, what the hashCode method does, depends on how it is implemented. The specification says that it has to comply with the equals function (whose behavior also depends on how it is implemented in the concrete type): If two objects are equal according to the equals method, they must return the same hash code. Since equals for String compares the values (not the references), both hashCode calls have to return the same value. And since hashCode returns an int, which is a primitive data type, your == comparison actually compares values and therefore works as expected.

Note: If you had done the same experiment for a class different from String that doesn't override the equals and hashCode methods (which is perfectly valid; it's just another way of defining equality on your type of object), it would have returned false for the hashCode comparison, too:

public class MyObject {

    private int value;

    public MyObject(MyObject toCopy) {
        this.value = toCopy.value;
    }   

    public MyObject(int value) {
        this.value = value;
    }

    public static void main(String[] args) {
        MyObject s1 = new MyObject(0);
        MyObject s2 = new MyObject(s1);
        System.out.println(s1 == s2);                       // false
        System.out.println(s1.hashCode() == s2.hashCode()); // false
    }
}

It all depends on how the programmer of the class defines which objects to consider equal.