Java String hashCode() Method

Java String hashCode() Method | Java provides a method called hashcode() to retrieve hash code for the strings, it is computed as:- s[0] * 31^(n-1) + s[1] * 31^(n-2) + … + s[n-1] where s[i] is the ith character, the ‘^’ carrot symbol indicates exponential and n is the length of the string.

The hash value is then the encrypted value of the hash function. This hashcode() method is mainly defined in the Object class which is the parent class for all Java classes. Java String class overrides the hashCode() method and provides its own implementation different from the Object class.

Here are some examples for hashCode() return value:-
String = “honey”
Hashcode value = 99461947
String = “kenmore”
Hashcode value = -824864855

String hashCode() Java

The signature of the hashCode() method of the Java string class is:- public int hashCode()
Parameters:- takes nothing as a parameter
Returns:- Returns the hash value of the string
Return type:- int

Java String hashcode() Implementation

The hashCode() method in the Java String class is implemented as follows:-

public int hashCode() {
   int h = hash;
   if (h == 0 && !hashIsZero) {
      h = isLatin1() ? StringLatin1.hashCode(value)
                     : StringUTF16.hashCode(value);
      if (h == 0) {
         hashIsZero = true;
      } else {
         hash = h;
      }
   }
   return h;
}

String hashCode() Java Example

Let us see some examples of the Java String hashCode() method to understand how it works, and by following which algorithm it returns the results.

class Main {
   public static void main(String[] args) {
      String str = "a";
      String str1 = "A";
      String str2 = "z";
      String str3 = "Z";
      String str4 = "0";
      String str5 = "9";

      System.out.println(str.hashCode());
      System.out.println(str1.hashCode());
      System.out.println(str2.hashCode());
      System.out.println(str3.hashCode());
      System.out.println(str4.hashCode());
      System.out.println(str5.hashCode());
   }
}

Output:

97
65
122
90
48
57

From the above program, we can observe that the return value is nothing but the ASCII value of the given character. From the algorithm:- s[0] * 31^(n-1) + s[1] * 31^(n-2) + … + s[n-1] for string = “A”, Hashcode = 65 * 31^(1-1) = 65 * 1 = 65. The ASCII value of ‘A’ is 65. See more:- Java program to find ASCII value. ASCII values of a-z, A-Z, and 0-9 are listed there.

public class Main {
   public static void main(String[] args) {
      String str = "Hi";
      String str1 = "Java";
      String str2 = "KnowProgram";
      String str3 = "#[email protected]";

      System.out.println(str.hashCode());
      System.out.println(str1.hashCode());
      System.out.println(str2.hashCode());
      System.out.println(str3.hashCode());
   }
}

Output:-

2337
2301506
2058267993
-617909819

The hashcode value of string = “Hi” is calculated as follows:- 72 * 31^(2-1) + 105 * 31^(2-2) = 72*31 + 105*1 = 2232 + 105 = 2337.

Similarly the hashcode value of string = “Java” can be calculated as follows:- 74 *31^(4-1) + 97 * 31^(4-2) + 118 * 31^(4-3) + 97 * 31^(4-4) = 74 * 31^3 + 97 * 31^2 + 118 * 31 + 97 = 74 * 29791 + 97 * 961 + 118 * 31 + 97 = 2204534 + 93217 + 3658 + 97 = 2301506

For the string “#[email protected]” the hashcode value is negative i.e. -617909819. Because the hashCode() value return type is int. Once the total value exceeds the Integer.MAX_VALUE (2^31 – 1) range then due to circular order it goes into negative values.

Since the hashcode value is calculated based on the algorithm therefore there can be cases where two strings can have the same hashcode. For example string, “FB” and “Ea” have the same hashcode.

public class Main {
   public static void main(String[] args) {
      String str = "FB";
      String str1 = "Ea";

      System.out.println(str.hashCode());
      System.out.println(str1.hashCode());
   }
}

Output:-

2236
2236

Similarly the strings “abyz”, & “abz[” have the same hashcode. And “tensada” & “friabili” have the same hashcode. Since the int range is between -2^31 to 2^31-1 therefore there can be infinite possibilities for strings having the same hashcode.

public class Main {
   public static void main(String[] args) {
      String str = "abyz";
      String str1 = "abz[";
      System.out.println(str.hashCode());
      System.out.println(str1.hashCode());

      String str2 = "tensada";
      String str3 = "friabili";
      System.out.println(str2.hashCode());
      System.out.println(str3.hashCode());
   }
}

Output:-

2987778
2987778
-1427101464
-1427101464

Java String hashCode Example

In this code, we compare two strings and find out whether they have the same hash value or not.

public class Main {
   public static void main(String[] args) {
      String str = "K";
      String str1 = "K";
      int hash1 = str.hashCode();
      int hash2 = str1.hashCode();
      if (hash1 == hash2) {
         System.out.println("Values Same");
      } else {
         System.out.println("Not Same");
      }

      String str2 = "a";
      String str3 = "A";
      int hash3 = str2.hashCode();
      int hash4 = str3.hashCode();
      if (hash3 == hash4) {
         System.out.println("Values Same");
      } else {
         System.out.println("Not Same");
      }

      String str4 = "HAPPY";
      String str5 = "Happy";
      int hash5 = str4.hashCode();
      int hash6 = str5.hashCode();
      if (hash5 == hash6) {
         System.out.println("Values Same");
      } else {
         System.out.println("Not Same");
      }

   }
}

Output:-

Values Same
Not Same
Not Same

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *