Proof why does java.lang.String.hashCode's implementation match its documentation?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The documented formula for String.hashCode() can look different from the loop used in the actual implementation, but they are mathematically the same. The reason is that the implementation evaluates the documented polynomial with Horner's method, which is a standard way to compute a polynomial efficiently.
The Documented Formula
The Java documentation defines the hash code of a string s of length n as:
using int arithmetic.
Two details matter:
- '
s[i]is the numeric value of theith UTF-16 code unit' - "using int arithmetic" means overflow is intentional and part of the definition
So the documentation does not describe an idealized infinite-precision formula. It describes exactly what Java should compute with 32-bit signed integer arithmetic.
The Implementation Form
The implementation is typically equivalent to this:
At first glance, this looks different from the documented sum of powers of 31. It is the same computation written in iterative form.
Why They Are Equal
Expand the loop one step at a time.
After processing the first character:
After processing the second character:
After the third character:
After the fourth character:
The pattern is now clear. After processing all n characters, the loop has produced:
That is exactly the documented formula.
Short Inductive Proof
You can state the proof formally by induction.
Induction claim: after processing the first k characters, h equals:
Base case, k = 1:
So the claim holds.
Inductive step:
Assume it holds for k. On the next iteration:
Substitute the induction hypothesis for h:
Distributing 31 shifts every exponent up by one, yielding exactly the formula for k + 1 characters. Therefore the claim holds for all k, including k = n.
Example with a Real String
Take "abc". Using character values 97, 98, and 99:
Documented formula:
Implementation loop:
Direct calculation:
Same result.
Common Pitfalls
The biggest mistake is reading ^ in the documentation as Java's XOR operator. In the documentation, ^ means mathematical exponentiation, not Java syntax.
Another common issue is forgetting the "using int arithmetic" part. Overflow is not a bug here; it is part of the specified behavior and helps explain why the implementation does not need special overflow handling.
Some explanations also say the loop is only an optimization. That is true, but incomplete. It is not merely a fast approximation. It is algebraically identical to the documented formula.
Finally, remember that String hashing works on UTF-16 code units, not abstract Unicode characters as users might think of them visually.
Summary
- The documentation describes a polynomial in powers of
31. - The implementation computes the same polynomial using Horner's method.
- A simple expansion or induction proof shows the two forms are identical.
- Overflow is expected because the specification explicitly uses
intarithmetic. - The loop is an exact implementation of the documented formula, not a different algorithm.
Related reading
- Proper usage of Java -D command-line parameters
- Proper way of streaming using ResponseEntity and making sure the InputStream gets
- Properly removing an Integer from a ListInteger
- Property 'security.basic.enabled' is Deprecated The security auto-configuration is no longer customizable
- Property 'spring.profiles.active' imported from location 'class path resource application-dev.yml' is invalid
- Provide Spring Boot git and build information via /actuator/info endpoint when using maven as a build tool
- Providing white space in a Swing GUI
- Proxy setting not working with Spring WebClient

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.