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.
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.
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; }
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 64578Below 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