Java's String hashCode is computed as a polynomial hash of the string's characters using the prime number 31 as the base. The choice of 31 is intentional: it's prime, and 31-1=30... actually 31 is chosen because it and 29 (31-2) are both primes. For long strings, the JVM can leverage CPU SIMD instructions. Since String is immutable, the hash is computed lazily on first access and cached in a field. However, this caching technically makes String mutable (it modifies `hash` and `hashIsZero` fields), which prevents String from being a Java value class.
•1m watch time
Sort: