1"use strict";(self.webpackChunkyolo=self.webpackChunkyolo||[]).push([["321"],{80245(e,n,a){a.r(n),a.d(n,{metadata:()=>t,default:()=>h,frontMatter:()=>i,contentTitle:()=>r,toc:()=>l,assets:()=>d});var t=JSON.parse('{"id":"notes/develop/java/java-string-hashcode-31","title":"String \u7684 hashCode \u4E2D\u7684 31","description":"\u4E3A\u4EC0\u4E48 Java \u4E2D String \u7684 hashCode \u65B9\u6CD5\u4E2D\u4F7F\u7528 31 \u8FD9\u4E2A\u201C\u9B54\u6570\u201D\uFF1F","source":"@site/docs/notes/develop/java/java-string-hashcode-31.md","sourceDirName":"notes/develop/java","slug":"/notes/develop/java/java-string-hashcode-31","permalink":"/docs/notes/develop/java/java-string-hashcode-31","draft":false,"unlisted":false,"tags":[{"inline":false,"label":"Java","permalink":"/docs/tags/java"}],"version":"current","lastUpdatedAt":1750741867000,"frontMatter":{"description":"\u4E3A\u4EC0\u4E48 Java \u4E2D String \u7684 hashCode \u65B9\u6CD5\u4E2D\u4F7F\u7528 31 \u8FD9\u4E2A\u201C\u9B54\u6570\u201D\uFF1F","tags":["java"]},"sidebar":"docsSidebar","previous":{"title":"Java/Android \u53D1\u5E03 library \u5305\u542B\u6E90\u7801\u548C\u6587\u6863","permalink":"/docs/notes/develop/java/java-publish-library-with-source-and-doc"},"next":{"title":"Mismatch in Module Imports between ES6 and CommonJS","permalink":"/docs/notes/develop/javascript/module-imports-mismatch-between-es6-and-commonjs"}}'),o=a(74848),s=a(28453);let i={description:"\u4E3A\u4EC0\u4E48 Java \u4E2D String \u7684 hashCode \u65B9\u6CD5\u4E2D\u4F7F\u7528 31 \u8FD9\u4E2A\u201C\u9B54\u6570\u201D\uFF1F",tags:["java"]},r="String \u7684 hashCode \u4E2D\u7684 31",d={},l=[];function c(e){let n={blockquote:"blockquote",code:"code",em:"em",h1:"h1",header:"header",p:"p",pre:"pre",strong:"strong",...(0,s.R)(),...e.components};return(0,o.jsxs)(o.Fragment,{children:[(0,o.jsx)(n.header,{children:(0,o.jsx)(n.h1,{id:"string-\u7684-hashcode-\u4E2D\u7684-31",children:"String \u7684 hashCode \u4E2D\u7684 31"})}),"\n",(0,o.jsxs)(n.p,{children:["Java 8 \u4E2D",(0,o.jsx)(n.code,{children:"String"}),"\u7C7B\u7684",(0,o.jsx)(n.code,{children:"hashCode()"}),"\u65B9\u6CD5\u5B9E\u73B0\u5982\u4E0B\uFF1A"]}),"\n",(0,o.jsx)(n.pre,{children:(0,o.jsx)(n.code,{className:"language-java",children:"public int hashCode() {\n int h = hash;\n if (h == 0 && value.length > 0) {\n char val[] = value;\n\n for (int i = 0; i < value.length; i++) {\n h = 31 * h + val[i];\n }\n hash = h;\n }\n return h;\n}\n"})}),"\n",(0,o.jsxs)(n.p,{children:["\u53EF\u4EE5\u770B\u5230\uFF0C\u5FAA\u73AF\u4E2D\u7684\u6BCF\u4E00\u6B65\u90FD\u5BF9\u4E0A\u4E00\u6B65\u7684\u7ED3\u679C\u4E58\u4EE5\u4E00\u4E2A\u7CFB\u6570",(0,o.jsx)(n.strong,{children:"31"}),"\uFF0C\u90A3\u4E48\uFF0C\u8FD9\u4E2A",(0,o.jsx)(n.strong,{children:"31"}),"\u53C8\u662F\u600E\u4E48\u6765\u7684\u5462\uFF1F\n\u5728 ",(0,o.jsx)(n.em,{children:"Effective Java"})," \u7B2C\u4E8C\u7248\u7684 ",(0,o.jsx)(n.em,{children:"Item 9: Always override hashCode when you override equals"})," \u4E2D\u6211\u4EEC\u627E\u5230\u4E86\u7B54\u6848\uFF1A"]}),"\n",(0,o.jsxs)(n.blockquote,{children:["\n",(0,o.jsxs)(n.p,{children:["The value 31 was chosen because it is an odd prime. If it were even and the multiplication overflowed, information would be lost, as multiplication by 2 is equivalent to shifting. The advantage of using a prime is less clear, but it is traditional. A nice property of 31 is that the multiplication can be replaced by a shift and a subtraction for better performance: ",(0,o.jsx)(n.code,{children:"31 * i == (i << 5) - i"}),". Modern VMs do this sort of optimization automatically."]}),"\n"]})]})}function h(e={}){let{wrapper:n}={...(0,s.R)(),...e.components};return n?(0,o.jsx)(n,{...e,children:(0,o.jsx)(c,{...e})}):c(e)}},28453(e,n,a){a.d(n,{R:()=>i,x:()=>r});var t=a(96540);let o={},s=t.createContext(o);function i(e){let n=t.useContext(s);return t.useMemo(function(){return"function"==typeof e?e(n):{...n,...e}},[n,e])}function r(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(o):e.components||o:i(e.components),t.createElement(s.Provider,{value:n},e.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.