JavaDevNotes.com

Java String hashCode

Hash code in Java is very important specially with the introduction of the Java Collections API. Many classes in the Collections API uses hash code as a convention. So what exactly is a hash code? It is an integer representation of a specific Object instance. The assumption is that this integer value should be the same whenever the hashCode() method is invoked on the same Object more than once during an execution of a Java application. The hashCode method must consistently provide the same value provided that no internal data used in equals is modified on the Object. We should not worry about this on Immutable objects as internal data is not change throughout it's lifetime. In addition, if two objects are equal using the equals method of the class, then the hashCode of both should return the same value. This implies that the hashCode should be based on the data used in equals method. Lastly, it is not required that two unequal Objects should have different hashCode. Since hashCode is common to all classes as it is implemented in the Object class, we will discuss on how it is implemented in the String class of Java. This post will explain and give examples on Java String HashCode.

Java String hashCode Syntax/Signature

The hashCode() method of the String class in Java is used to get the hashCode value for the specific String instance. This will return an integer value that will not change throughout the lifetime of the String, as it is an immutable. The signature or Syntax of the method is:

public int hashCode()

As shown in the signature above, this method takes no argument and it returns a primitive int value.

Java String hashCode Algorithm

Visiting the documentation of the String class, this is how the value of the hashCode should be computed for String.

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

Where s[0] to s[n-1] are the individual characters in the String of length n. Each item is multiplied by the prime number 31 raised to some power. And the sum of all terms becomes the hashCode.

Java String hashCode Implementation

Visiting the source code of Java String, below is the implementation of the method hashCode as described in the algorithm above:

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;
}

Java String hashCode Example

We show below some simple examples of hashCode in Java. Below will show that hashCode will return the same value regardless of how many times we invoke it.

String str = "HelloWorld";
System.out.println(str.hashCode());
System.out.println(str.hashCode());
System.out.println(str.hashCode());

The output is the same number for all three invocation.

439329280
439329280
439329280

But if we assign a new instance to the String with a different content, the value will differ.

String str = "HelloWorld";
System.out.println(str.hashCode());
System.out.println(str.hashCode());
str = "ABC";
System.out.println(str.hashCode());

The first two hashCode is the same because it is the same String "HelloWorld", while the third is different because it's a different instance with value "ABC". The output is shown below.

439329280
439329280
64578

Below demonstrats that same value will result to same hashCode value too.
String stringA = "abcXYZ";
String stringB = "abc";
String stringC = "XYZ";
String stringD = stringB + stringC;
System.out.println(stringA.hashCode() == stringB.hashCode());
System.out.println(stringA.hashCode() == stringC.hashCode());
System.out.println(stringA.hashCode() == stringD.hashCode());
As "abcXYZ" is not the same as "abc", the first line output is false. And because "abcXYZ" is not the same as "XYZ", the second line output is also false. The third line will output true because stringA and StringD have same values. Expected output is shown below:
false
false
true