Take the 2-minute tour ×

Can someone tell me what the maximum and minimum int values the Java String.hashCode() method can return?

share|improve this question

3 Answers 3

up vote 4 down vote accepted

String.hashCode () return an int calculated by using the below formula:

public int hashCode()

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 i-th character of the string, n is the length of the string, and ^ indicates exponentiation. (The hash value of the empty string is zero.)

The minimum and max value can be found with the below constants.

 System.out.println (java.lang.Integer.MAX_VALUE); //  2147483647
 System.out.println (java.lang.Integer.MIN_VALUE); // -2147483648
share|improve this answer
+1 You don't need to include the java.lang package IMHO. –  Peter Lawrey Jul 14 '12 at 7:36

From the documentation, given that the power to which something is raised depends on the length of the String (which is practically unlimited, as far as I know), I would say that the maximum and minimum must be Integer.MAX_VALUE (2^31 - 1) and Integer.MIN_VALUE (-2^31), respectively.

share|improve this answer


share|improve this answer

Your Answer


