<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/how-is-string-hash-code-calculated---cracking-the-java-coding-interview-javacoding-javatips-ydobzmevm" -->

---
title: How is String hash code calculated? - Cracking the Java...
description: Java&#x27;s String hashCode is computed as a polynomial hash of the string&#x27;s characters using the prime number 31 as the base. The choice of 31 is intentional: it&#x27;s...
canonical: https://daily.dev/posts/how-is-string-hash-code-calculated---cracking-the-java-coding-interview-javacoding-javatips-ydobzmevm
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: How is String hash code calculated? - Cracking the Java Coding Interview #javacoding #javatips | daily.dev
og:description: Java&#x27;s String hashCode is computed as a polynomial hash of the string&#x27;s characters using the prime number 31 as the base. The choice of 31 is intentional: it&#x27;s...
og:url: https://daily.dev/posts/how-is-string-hash-code-calculated---cracking-the-java-coding-interview-javacoding-javatips-ydobzmevm
og:image: https://api.daily.dev/og/posts/YdObZmEVm.png
og:image:alt: How is String hash code calculated? - Cracking the Java Coding Interview #javacoding #javatips
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# How is String hash code calculated? - Cracking the Java Coding Interview #javacoding #javatips

**[Java \(Official Oracle\)](https://daily.dev/sources/java-oracle)** · 1 min read · 0 upvotes · 0 comments

## Summary

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.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.youtube.com/watch?v=UEkmf8cTyYU>

---

Tags: [#java](https://daily.dev/tags/java)

[View this post on daily.dev](https://daily.dev/posts/how-is-string-hash-code-calculated---cracking-the-java-coding-interview-javacoding-javatips-ydobzmevm)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"How is String hash code calculated? - Cracking the Java Coding Interview #javacoding #javatips","url":"https://daily.dev/posts/how-is-string-hash-code-calculated---cracking-the-java-coding-interview-javacoding-javatips-ydobzmevm","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/how-is-string-hash-code-calculated---cracking-the-java-coding-interview-javacoding-javatips-ydobzmevm"},"datePublished":"2026-03-16T08:13:28.244Z","dateModified":"2026-03-16T08:19:16.573Z","description":"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...","image":"https://i.ytimg.com/vi/UEkmf8cTyYU/sddefault.jpg","thumbnailUrl":"https://i.ytimg.com/vi/UEkmf8cTyYU/sddefault.jpg","isAccessibleForFree":true,"articleSection":"Java (Official Oracle)","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Java (Official Oracle)","logo":"https://media.daily.dev/image/upload/s--SM9hIYUY--/f_auto,q_auto/v1773648791/logos/java-oracle?_a=BAMAMiiu0","url":"https://daily.dev/sources/java-oracle"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/how-is-string-hash-code-calculated---cracking-the-java-coding-interview-javacoding-javatips-ydobzmevm","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"java","timeRequired":"PT1M","video":{"@type":"VideoObject","name":"How is String hash code calculated? - Cracking the Java Coding Interview #javacoding #javatips","description":"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...","thumbnailUrl":"https://i.ytimg.com/vi/UEkmf8cTyYU/sddefault.jpg","uploadDate":"2026-03-16T08:13:28.244Z","duration":"PT1M","url":"https://api.daily.dev/r/YdObZmEVm","embedUrl":"https://www.youtube.com/embed/UEkmf8cTyYU"}}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Java (Official Oracle)","item":"https://daily.dev/sources/java-oracle"},{"@type":"ListItem","position":3,"name":"How is String hash code calculated? - Cracking the Java Coding Interview #javacoding #javatips"}]}
```

