{"id":57082,"date":"2025-08-15T07:39:42","date_gmt":"2025-08-14T23:39:42","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/57082.html"},"modified":"2025-08-15T07:39:42","modified_gmt":"2025-08-14T23:39:42","slug":"java%e4%b8%ad%e7%9a%84%e5%b8%b8%e9%87%8f%e6%b1%a0%e6%b7%b1%e5%ba%a6%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/57082.html","title":{"rendered":"Java\u4e2d\u7684\u5e38\u91cf\u6c60\u6df1\u5ea6\u89e3\u6790"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814233938-689e73ba83273.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<\/p>\n<h4>\u6587\u7ae0\u76ee\u5f55<\/h4>\n<ul>\n<li>\n<ul>\n<li>\u4e00\u3001\u5e38\u91cf\u6c60\u7684\u57fa\u672c\u6982\u5ff5<\/li>\n<li>\n<ul>\n<li>1.1 \u5b9a\u4e49\u4e0e\u4f5c\u7528<\/li>\n<li>1.2 \u5e38\u91cf\u6c60\u7684\u4f4d\u7f6e<\/li>\n<\/ul>\n<\/li>\n<li>\u4e8c\u3001\u7c7b\u6587\u4ef6\u5e38\u91cf\u6c60\u7ed3\u6784<\/li>\n<li>\n<ul>\n<li>2.1 \u5e38\u91cf\u6c60\u7684\u7ec4\u7ec7\u65b9\u5f0f<\/li>\n<li>2.2 \u5e38\u91cf\u7c7b\u578b\u5206\u7c7b&#xff08;JVM\u89c4\u830317\u79cd&#xff09;<\/li>\n<li>2.3 \u5e38\u91cf\u6c60\u793a\u4f8b\u5206\u6790<\/li>\n<\/ul>\n<\/li>\n<li>\u4e09\u3001\u8fd0\u884c\u65f6\u5e38\u91cf\u6c60<\/li>\n<li>\n<ul>\n<li>3.1 \u7c7b\u52a0\u8f7d\u65f6\u7684\u8f6c\u6362<\/li>\n<li>3.2 \u52a8\u6001\u7279\u6027<\/li>\n<\/ul>\n<\/li>\n<li>\u56db\u3001\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\u7684\u7279\u6b8a\u5b9e\u73b0<\/li>\n<li>\n<ul>\n<li>4.1 \u5b57\u7b26\u4e32\u6c60\u7684\u4f4d\u7f6e<\/li>\n<li>4.2 \u5b57\u7b26\u4e32\u9a7b\u7559\u673a\u5236<\/li>\n<li>4.3 \u5b57\u7b26\u4e32\u62fc\u63a5\u4f18\u5316<\/li>\n<\/ul>\n<\/li>\n<li>\u4e94\u3001\u5e38\u91cf\u6c60\u7684\u8bbf\u95ee\u673a\u5236<\/li>\n<li>\n<ul>\n<li>5.1 \u5b57\u8282\u7801\u6307\u4ee4<\/li>\n<li>5.2 \u8bbf\u95ee\u8fc7\u7a0b\u793a\u4f8b<\/li>\n<\/ul>\n<\/li>\n<li>\u516d\u3001\u5e38\u91cf\u6c60\u7684\u6027\u80fd\u5f71\u54cd<\/li>\n<li>\n<ul>\n<li>6.1 \u5185\u5b58\u4f18\u5316<\/li>\n<li>6.2 \u8bbf\u95ee\u6548\u7387<\/li>\n<\/ul>\n<\/li>\n<li>\u4e03\u3001\u5e38\u91cf\u6c60\u76f8\u5173\u5f02\u5e38<\/li>\n<li>\n<ul>\n<li>7.1 \u5e38\u89c1\u5f02\u5e38\u7c7b\u578b<\/li>\n<li>7.2 \u793a\u4f8b\u573a\u666f<\/li>\n<\/ul>\n<\/li>\n<li>\u516b\u3001\u5e38\u91cf\u6c60\u5de5\u5177\u4e0e\u8c03\u8bd5<\/li>\n<li>\n<ul>\n<li>8.1 \u67e5\u770b\u5e38\u91cf\u6c60<\/li>\n<li>8.2 \u6027\u80fd\u5206\u6790<\/li>\n<\/ul>\n<\/li>\n<li>\u4e5d\u3001\u5e38\u91cf\u6c60\u7684\u6700\u4f73\u5b9e\u8df5<\/li>\n<li>\n<ul>\n<li>9.1 \u7f16\u7801\u5efa\u8bae<\/li>\n<li>9.2 \u914d\u7f6e\u8c03\u4f18<\/li>\n<\/ul>\n<\/li>\n<li>\u5341\u3001\u5e38\u91cf\u6c60\u7684\u5185\u90e8\u5b9e\u73b0&#xff08;HotSpot&#xff09;<\/li>\n<li>\n<ul>\n<li>10.1 \u5b58\u50a8\u7ed3\u6784<\/li>\n<li>10.2 \u89e3\u6790\u8fc7\u7a0b<\/li>\n<\/ul>\n<\/li>\n<li>\u5341\u4e00\u3001\u5e38\u91cf\u6c60\u4e0eJava\u65b0\u7279\u6027<\/li>\n<li>\n<ul>\n<li>11.1 \u52a8\u6001\u5e38\u91cf&#xff08;JEP 309&#xff09;<\/li>\n<li>11.2 \u8bb0\u5f55\u7c7b&#xff08;Record&#xff09;<\/li>\n<\/ul>\n<\/li>\n<li>\u5341\u4e8c\u3001\u603b\u7ed3<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u5e38\u91cf\u6c60(Constant Pool)\u662fJava\u865a\u62df\u673a(JVM)\u4e2d\u4e00\u4e2a\u81f3\u5173\u91cd\u8981\u7684\u6570\u636e\u7ed3\u6784&#xff0c;\u5b83\u5b58\u50a8\u4e86\u7c7b\u6587\u4ef6\u4e2d\u6240\u6709\u7684\u7b26\u53f7\u5f15\u7528\u548c\u5b57\u9762\u91cf\u5e38\u91cf\u3002\u672c\u6587\u5c06\u5168\u9762\u5256\u6790Java\u5e38\u91cf\u6c60\u7684\u7ec4\u6210\u7ed3\u6784\u3001\u7c7b\u578b\u5206\u7c7b\u3001\u8bbf\u95ee\u673a\u5236\u4ee5\u53ca\u8fd0\u884c\u65f6\u884c\u4e3a&#xff0c;\u5e2e\u52a9\u5f00\u53d1\u8005\u6df1\u5165\u7406\u89e3\u8fd9\u4e00\u6838\u5fc3\u6982\u5ff5\u3002<\/p>\n<h3>\u4e00\u3001\u5e38\u91cf\u6c60\u7684\u57fa\u672c\u6982\u5ff5<\/h3>\n<h4>1.1 \u5b9a\u4e49\u4e0e\u4f5c\u7528<\/h4>\n<p>\u5e38\u91cf\u6c60\u662fJava\u7c7b\u6587\u4ef6\u4e2d\u7684\u4e00\u4e2a\u9759\u6001\u5b58\u50a8\u533a\u57df&#xff0c;\u5b83\u5305\u542b\u4e86&#xff1a;<\/p>\n<ul>\n<li>\u7c7b\u3001\u63a5\u53e3\u3001\u5b57\u6bb5\u548c\u65b9\u6cd5\u7684\u7b26\u53f7\u5f15\u7528<\/li>\n<li>\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u548c\u57fa\u672c\u7c7b\u578b\u5e38\u91cf<\/li>\n<li>\u65b9\u6cd5\u548c\u5b57\u6bb5\u7684\u63cf\u8ff0\u4fe1\u606f<\/li>\n<li>\u52a8\u6001\u8ba1\u7b97\u4f7f\u7528\u7684\u5143\u6570\u636e<\/li>\n<\/ul>\n<p>  #mermaid-svg-GKDsMTBnSxBgjj8b {font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-GKDsMTBnSxBgjj8b .error-icon{fill:#552222;}#mermaid-svg-GKDsMTBnSxBgjj8b .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-GKDsMTBnSxBgjj8b .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-GKDsMTBnSxBgjj8b .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-GKDsMTBnSxBgjj8b .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-GKDsMTBnSxBgjj8b .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-GKDsMTBnSxBgjj8b .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-GKDsMTBnSxBgjj8b .marker{fill:#333333;stroke:#333333;}#mermaid-svg-GKDsMTBnSxBgjj8b .marker.cross{stroke:#333333;}#mermaid-svg-GKDsMTBnSxBgjj8b svg{font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-GKDsMTBnSxBgjj8b .label{font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;color:#333;}#mermaid-svg-GKDsMTBnSxBgjj8b .cluster-label text{fill:#333;}#mermaid-svg-GKDsMTBnSxBgjj8b .cluster-label span{color:#333;}#mermaid-svg-GKDsMTBnSxBgjj8b .label text,#mermaid-svg-GKDsMTBnSxBgjj8b span{fill:#333;color:#333;}#mermaid-svg-GKDsMTBnSxBgjj8b .node rect,#mermaid-svg-GKDsMTBnSxBgjj8b .node circle,#mermaid-svg-GKDsMTBnSxBgjj8b .node ellipse,#mermaid-svg-GKDsMTBnSxBgjj8b .node polygon,#mermaid-svg-GKDsMTBnSxBgjj8b .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-GKDsMTBnSxBgjj8b .node .label{text-align:center;}#mermaid-svg-GKDsMTBnSxBgjj8b .node.clickable{cursor:pointer;}#mermaid-svg-GKDsMTBnSxBgjj8b .arrowheadPath{fill:#333333;}#mermaid-svg-GKDsMTBnSxBgjj8b .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-GKDsMTBnSxBgjj8b .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-GKDsMTBnSxBgjj8b .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-GKDsMTBnSxBgjj8b .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-GKDsMTBnSxBgjj8b .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-GKDsMTBnSxBgjj8b .cluster text{fill:#333;}#mermaid-svg-GKDsMTBnSxBgjj8b .cluster span{color:#333;}#mermaid-svg-GKDsMTBnSxBgjj8b div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-GKDsMTBnSxBgjj8b :root{&#8211;mermaid-font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;}<\/p>\n<p>         <span id=\"L-L-A-B\" class=\"edgeLabel L-LS-A&#039; L-LE-B\"><\/span><\/p>\n<p>         <span id=\"L-L-A-C\" class=\"edgeLabel L-LS-A&#039; L-LE-C\"><\/span><\/p>\n<p>         <span id=\"L-L-A-D\" class=\"edgeLabel L-LS-A&#039; L-LE-D\"><\/span><\/p>\n<p>          \u5e38\u91cf\u6c60<\/p>\n<p>          \u5b57\u9762\u91cf<\/p>\n<p>          \u7b26\u53f7\u5f15\u7528<\/p>\n<p>          \u5143\u6570\u636e<\/p>\n<h4>1.2 \u5e38\u91cf\u6c60\u7684\u4f4d\u7f6e<\/h4>\n<li>\u7c7b\u6587\u4ef6\u5e38\u91cf\u6c60&#xff1a;\u5b58\u50a8\u5728.class\u6587\u4ef6\u4e2d&#xff08;\u7f16\u8bd1\u671f\u751f\u6210&#xff09;<\/li>\n<li>\u8fd0\u884c\u65f6\u5e38\u91cf\u6c60&#xff1a;\u7c7b\u52a0\u8f7d\u540e\u8fdb\u5165\u65b9\u6cd5\u533a&#xff08;JVM\u5185\u5b58&#xff09;<\/li>\n<li>\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60&#xff1a;\u7279\u6b8a\u5b9e\u73b0&#xff0c;\u5b58\u50a8\u5b57\u7b26\u4e32\u5bf9\u8c61&#xff08;\u5806\u4e2d&#xff09;<\/li>\n<h3>\u4e8c\u3001\u7c7b\u6587\u4ef6\u5e38\u91cf\u6c60\u7ed3\u6784<\/h3>\n<h4>2.1 \u5e38\u91cf\u6c60\u7684\u7ec4\u7ec7\u65b9\u5f0f<\/h4>\n<p>.class\u6587\u4ef6\u4e2d\u7684\u5e38\u91cf\u6c60\u91c7\u7528\u7d22\u5f15\u8bbf\u95ee\u7684\u8868\u7ed3\u6784&#xff1a;<\/p>\n<ul>\n<li>\u5e38\u91cf\u6c60\u8ba1\u6570\u5668&#xff1a;u2\u7c7b\u578b&#xff0c;\u8bb0\u5f55\u5e38\u91cf\u6570\u91cf&#xff08;\u4ece1\u5f00\u59cb\u8ba1\u6570&#xff09;<\/li>\n<li>\u5e38\u91cf\u6c60\u8868&#xff1a;\u7531\u591a\u4e2acp_info\u7ed3\u6784\u7ec4\u6210<\/li>\n<\/ul>\n<p><span class=\"token comment\">\/\/ \u7c7b\u6587\u4ef6\u5e38\u91cf\u6c60\u7684\u4f2a\u7ed3\u6784<\/span><br \/>\n<span class=\"token class-name\">ClassFile<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    u4 magic<span class=\"token punctuation\">;<\/span><br \/>\n    u2 minor_version<span class=\"token punctuation\">;<\/span><br \/>\n    u2 major_version<span class=\"token punctuation\">;<\/span><br \/>\n    u2 constant_pool_count<span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u5e38\u91cf\u6c60\u5927\u5c0f<\/span><br \/>\n    cp_info constant_pool<span class=\"token punctuation\">[<\/span>constant_pool_count<span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u5e38\u91cf\u6c60\u8868<\/span><br \/>\n    <span class=\"token comment\">\/\/ &#8230;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h4>2.2 \u5e38\u91cf\u7c7b\u578b\u5206\u7c7b&#xff08;JVM\u89c4\u830317\u79cd&#xff09;<\/h4>\n<table>\n<tr>\u7c7b\u578b\u6807\u5fd7\u5e38\u91cf\u7c7b\u578b\u63cf\u8ff0<\/tr>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>CONSTANT_Utf8<\/td>\n<td>UTF-8\u7f16\u7801\u5b57\u7b26\u4e32<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>CONSTANT_Integer<\/td>\n<td>int\u5b57\u9762\u91cf<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>CONSTANT_Float<\/td>\n<td>float\u5b57\u9762\u91cf<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>CONSTANT_Long<\/td>\n<td>long\u5b57\u9762\u91cf<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>CONSTANT_Double<\/td>\n<td>double\u5b57\u9762\u91cf<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>CONSTANT_Class<\/td>\n<td>\u7c7b\/\u63a5\u53e3\u7b26\u53f7\u5f15\u7528<\/td>\n<\/tr>\n<tr>\n<td>8<\/td>\n<td>CONSTANT_String<\/td>\n<td>\u5b57\u7b26\u4e32\u5b57\u9762\u91cf<\/td>\n<\/tr>\n<tr>\n<td>9<\/td>\n<td>CONSTANT_Fieldref<\/td>\n<td>\u5b57\u6bb5\u7b26\u53f7\u5f15\u7528<\/td>\n<\/tr>\n<tr>\n<td>10<\/td>\n<td>CONSTANT_Methodref<\/td>\n<td>\u65b9\u6cd5\u7b26\u53f7\u5f15\u7528<\/td>\n<\/tr>\n<tr>\n<td>11<\/td>\n<td>CONSTANT_InterfaceMethodref<\/td>\n<td>\u63a5\u53e3\u65b9\u6cd5\u5f15\u7528<\/td>\n<\/tr>\n<tr>\n<td>12<\/td>\n<td>CONSTANT_NameAndType<\/td>\n<td>\u540d\u79f0\u548c\u63cf\u8ff0\u7b26<\/td>\n<\/tr>\n<tr>\n<td>15<\/td>\n<td>CONSTANT_MethodHandle<\/td>\n<td>\u65b9\u6cd5\u53e5\u67c4<\/td>\n<\/tr>\n<tr>\n<td>16<\/td>\n<td>CONSTANT_MethodType<\/td>\n<td>\u65b9\u6cd5\u7c7b\u578b<\/td>\n<\/tr>\n<tr>\n<td>17<\/td>\n<td>CONSTANT_Dynamic<\/td>\n<td>\u52a8\u6001\u8ba1\u7b97\u5e38\u91cf<\/td>\n<\/tr>\n<tr>\n<td>18<\/td>\n<td>CONSTANT_InvokeDynamic<\/td>\n<td>\u52a8\u6001\u65b9\u6cd5\u8c03\u7528<\/td>\n<\/tr>\n<tr>\n<td>19<\/td>\n<td>CONSTANT_Module<\/td>\n<td>\u6a21\u5757\u5f15\u7528<\/td>\n<\/tr>\n<tr>\n<td>20<\/td>\n<td>CONSTANT_Package<\/td>\n<td>\u5305\u5f15\u7528<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>2.3 \u5e38\u91cf\u6c60\u793a\u4f8b\u5206\u6790<\/h4>\n<p>\u4f7f\u7528javap -v\u67e5\u770b\u7c7b\u6587\u4ef6\u5e38\u91cf\u6c60&#xff1a;<\/p>\n<p><span class=\"token comment\">\/\/ \u793a\u4f8b\u7c7b<\/span><br \/>\n<span class=\"token keyword\">public<\/span> <span class=\"token keyword\">class<\/span> <span class=\"token class-name\">ConstantPoolDemo<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">private<\/span> <span class=\"token keyword\">final<\/span> <span class=\"token keyword\">int<\/span> MAX <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">100<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">private<\/span> <span class=\"token class-name\">String<\/span> name <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;Java&#034;<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token keyword\">public<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span>name<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u53cd\u7f16\u8bd1\u8f93\u51fa&#xff08;\u90e8\u5206&#xff09;&#xff1a;<\/p>\n<p>Constant pool:<br \/>\n   #1 &#061; Methodref          #6.#20         \/\/ java\/lang\/Object.&#034;&lt;init&gt;&#034;:()V<br \/>\n   #2 &#061; String             #21            \/\/ Java<br \/>\n   #3 &#061; Fieldref           #5.#22         \/\/ ConstantPoolDemo.name:Ljava\/lang\/String;<br \/>\n   #4 &#061; Fieldref           #5.#23         \/\/ ConstantPoolDemo.MAX:I<br \/>\n   #5 &#061; Class              #24            \/\/ ConstantPoolDemo<br \/>\n   #6 &#061; Class              #25            \/\/ java\/lang\/Object<br \/>\n   #7 &#061; Utf8               MAX<br \/>\n   #8 &#061; Utf8               I<br \/>\n   #9 &#061; Utf8               ConstantValue<br \/>\n  #10 &#061; Integer            100<br \/>\n  #21 &#061; Utf8               Java<br \/>\n  #22 &#061; NameAndType        #7:#8          \/\/ name:Ljava\/lang\/String;<br \/>\n  #23 &#061; NameAndType        #26:#8         \/\/ MAX:I<\/p>\n<h3>\u4e09\u3001\u8fd0\u884c\u65f6\u5e38\u91cf\u6c60<\/h3>\n<h4>3.1 \u7c7b\u52a0\u8f7d\u65f6\u7684\u8f6c\u6362<\/h4>\n<p>\u5f53\u7c7b\u88ab\u52a0\u8f7d\u65f6&#xff0c;\u7c7b\u6587\u4ef6\u5e38\u91cf\u6c60\u8f6c\u6362\u4e3a\u8fd0\u884c\u65f6\u5e38\u91cf\u6c60&#xff1a;<\/p>\n<li>\u7b26\u53f7\u5f15\u7528\u88ab\u89e3\u6790\u4e3a\u76f4\u63a5\u5f15\u7528<\/li>\n<li>\u5e38\u91cf\u503c\u88ab\u8f6c\u6362\u4e3a\u8fd0\u884c\u65f6\u8868\u793a<\/li>\n<li>\u52a8\u6001\u5e38\u91cf\u88ab\u8ba1\u7b97<\/li>\n<p>  #mermaid-svg-rbNBhM3TFhfck2AM {font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-rbNBhM3TFhfck2AM .error-icon{fill:#552222;}#mermaid-svg-rbNBhM3TFhfck2AM .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-rbNBhM3TFhfck2AM .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-rbNBhM3TFhfck2AM .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-rbNBhM3TFhfck2AM .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-rbNBhM3TFhfck2AM .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-rbNBhM3TFhfck2AM .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-rbNBhM3TFhfck2AM .marker{fill:#333333;stroke:#333333;}#mermaid-svg-rbNBhM3TFhfck2AM .marker.cross{stroke:#333333;}#mermaid-svg-rbNBhM3TFhfck2AM svg{font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-rbNBhM3TFhfck2AM .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-rbNBhM3TFhfck2AM text.actor&gt;tspan{fill:black;stroke:none;}#mermaid-svg-rbNBhM3TFhfck2AM .actor-line{stroke:grey;}#mermaid-svg-rbNBhM3TFhfck2AM .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-rbNBhM3TFhfck2AM .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-rbNBhM3TFhfck2AM #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-rbNBhM3TFhfck2AM .sequenceNumber{fill:white;}#mermaid-svg-rbNBhM3TFhfck2AM #sequencenumber{fill:#333;}#mermaid-svg-rbNBhM3TFhfck2AM #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-rbNBhM3TFhfck2AM .messageText{fill:#333;stroke:#333;}#mermaid-svg-rbNBhM3TFhfck2AM .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-rbNBhM3TFhfck2AM .labelText,#mermaid-svg-rbNBhM3TFhfck2AM .labelText&gt;tspan{fill:black;stroke:none;}#mermaid-svg-rbNBhM3TFhfck2AM .loopText,#mermaid-svg-rbNBhM3TFhfck2AM .loopText&gt;tspan{fill:black;stroke:none;}#mermaid-svg-rbNBhM3TFhfck2AM .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-rbNBhM3TFhfck2AM .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-rbNBhM3TFhfck2AM .noteText,#mermaid-svg-rbNBhM3TFhfck2AM .noteText&gt;tspan{fill:black;stroke:none;}#mermaid-svg-rbNBhM3TFhfck2AM .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-rbNBhM3TFhfck2AM .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-rbNBhM3TFhfck2AM .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-rbNBhM3TFhfck2AM .actorPopupMenu{position:absolute;}#mermaid-svg-rbNBhM3TFhfck2AM .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 \/ 0.4));}#mermaid-svg-rbNBhM3TFhfck2AM .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-rbNBhM3TFhfck2AM .actor-man circle,#mermaid-svg-rbNBhM3TFhfck2AM line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-rbNBhM3TFhfck2AM :root{&#8211;mermaid-font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;}<\/p>\n<p>      ClassLoader<\/p>\n<p>      JVM<\/p>\n<p>   \u52a0\u8f7d.class\u6587\u4ef6<\/p>\n<p>   \u89e3\u6790\u5e38\u91cf\u6c60\u8868<\/p>\n<p>   \u8f6c\u6362\u7b26\u53f7\u5f15\u7528<\/p>\n<p>   \u521b\u5efa\u8fd0\u884c\u65f6\u5e38\u91cf\u6c60<\/p>\n<p>     ClassLoader<\/p>\n<p>     JVM<\/p>\n<h4>3.2 \u52a8\u6001\u7279\u6027<\/h4>\n<p>\u8fd0\u884c\u65f6\u5e38\u91cf\u6c60\u76f8\u6bd4\u7c7b\u6587\u4ef6\u5e38\u91cf\u6c60&#xff1a;<\/p>\n<ul>\n<li>\u652f\u6301\u52a8\u6001\u6dfb\u52a0\u5e38\u91cf&#xff08;\u5982String.intern()&#xff09;<\/li>\n<li>\u53ef\u4ee5\u5305\u542b\u8fd0\u884c\u65f6\u8ba1\u7b97\u7684\u5e38\u91cf<\/li>\n<li>\u5177\u6709\u5185\u5b58\u5730\u5740\u5f15\u7528\u800c\u975e\u6587\u4ef6\u504f\u79fb\u91cf<\/li>\n<\/ul>\n<h3>\u56db\u3001\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\u7684\u7279\u6b8a\u5b9e\u73b0<\/h3>\n<h4>4.1 \u5b57\u7b26\u4e32\u6c60\u7684\u4f4d\u7f6e<\/h4>\n<ul>\n<li>JDK7\u524d&#xff1a;\u6c38\u4e45\u4ee3&#xff08;\u65b9\u6cd5\u533a\u7684\u4e00\u90e8\u5206&#xff09;<\/li>\n<li>JDK7&#043;&#xff1a;\u79fb\u52a8\u5230\u5806\u5185\u5b58<\/li>\n<\/ul>\n<h4>4.2 \u5b57\u7b26\u4e32\u9a7b\u7559\u673a\u5236<\/h4>\n<p><span class=\"token class-name\">String<\/span> s1 <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;Java&#034;<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u4f7f\u7528\u5e38\u91cf\u6c60<\/span><br \/>\n<span class=\"token class-name\">String<\/span> s2 <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">String<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;Java&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u65b0\u5efa\u5bf9\u8c61<\/span><br \/>\n<span class=\"token class-name\">String<\/span> s3 <span class=\"token operator\">&#061;<\/span> s2<span class=\"token punctuation\">.<\/span><span class=\"token function\">intern<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u5c1d\u8bd5\u52a0\u5165\u5e38\u91cf\u6c60<\/span><\/p>\n<p><span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span>s1 <span class=\"token operator\">&#061;&#061;<\/span> s3<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ true<\/span><\/p>\n<h4>4.3 \u5b57\u7b26\u4e32\u62fc\u63a5\u4f18\u5316<\/h4>\n<p><span class=\"token comment\">\/\/ JDK5&#043; \u7f16\u8bd1\u4f18\u5316<\/span><br \/>\n<span class=\"token class-name\">String<\/span> a <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;Hello&#034;<\/span> <span class=\"token operator\">&#043;<\/span> <span class=\"token string\">&#034;World&#034;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/ \u7f16\u8bd1\u4e3a&#xff1a;<\/span><br \/>\n<span class=\"token class-name\">String<\/span> a <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;HelloWorld&#034;<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u8fd0\u884c\u65f6\u62fc\u63a5&#xff08;\u4e0d\u4f18\u5316&#xff09;<\/span><br \/>\n<span class=\"token class-name\">String<\/span> b <span class=\"token operator\">&#061;<\/span> s1 <span class=\"token operator\">&#043;<\/span> s2<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/ \u7b49\u6548\u4e8e&#xff1a;<\/span><br \/>\n<span class=\"token class-name\">String<\/span> b <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">StringBuilder<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">append<\/span><span class=\"token punctuation\">(<\/span>s1<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">append<\/span><span class=\"token punctuation\">(<\/span>s2<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">toString<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<h3>\u4e94\u3001\u5e38\u91cf\u6c60\u7684\u8bbf\u95ee\u673a\u5236<\/h3>\n<h4>5.1 \u5b57\u8282\u7801\u6307\u4ee4<\/h4>\n<p>JVM\u901a\u8fc7\u7279\u5b9a\u6307\u4ee4\u8bbf\u95ee\u5e38\u91cf\u6c60&#xff1a;<\/p>\n<table>\n<tr>\u6307\u4ee4\u4f5c\u7528\u793a\u4f8b<\/tr>\n<tbody>\n<tr>\n<td>ldc<\/td>\n<td>\u52a0\u8f7d\u5e38\u91cf<\/td>\n<td>ldc #2 (\u52a0\u8f7d\u5b57\u7b26\u4e32)<\/td>\n<\/tr>\n<tr>\n<td>ldc_w<\/td>\n<td>\u5bbd\u7d22\u5f15\u52a0\u8f7d<\/td>\n<td>ldc_w #65535<\/td>\n<\/tr>\n<tr>\n<td>ldc2_w<\/td>\n<td>\u52a0\u8f7dlong\/double<\/td>\n<td>ldc2_w #4<\/td>\n<\/tr>\n<tr>\n<td>getstatic<\/td>\n<td>\u83b7\u53d6\u9759\u6001\u5b57\u6bb5<\/td>\n<td>getstatic #3<\/td>\n<\/tr>\n<tr>\n<td>invokestatic<\/td>\n<td>\u8c03\u7528\u9759\u6001\u65b9\u6cd5<\/td>\n<td>invokestatic #5<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>5.2 \u8bbf\u95ee\u8fc7\u7a0b\u793a\u4f8b<\/h4>\n<p><span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;Hello&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u5bf9\u5e94\u5b57\u8282\u7801&#xff1a;<\/p>\n<p>0: getstatic     #2  \/\/ Field java\/lang\/System.out:Ljava\/io\/PrintStream;<br \/>\n3: ldc           #3  \/\/ String Hello<br \/>\n5: invokevirtual #4  \/\/ Method java\/io\/PrintStream.println:(Ljava\/lang\/String;)V<\/p>\n<h3>\u516d\u3001\u5e38\u91cf\u6c60\u7684\u6027\u80fd\u5f71\u54cd<\/h3>\n<h4>6.1 \u5185\u5b58\u4f18\u5316<\/h4>\n<ul>\n<li>\u5171\u4eab\u5e38\u91cf&#xff1a;\u76f8\u540c\u5b57\u9762\u91cf\u53ea\u5b58\u50a8\u4e00\u4efd<\/li>\n<li>\u51cf\u5c11\u91cd\u590d&#xff1a;\u7b26\u53f7\u5f15\u7528\u590d\u7528<\/li>\n<li>\u5ef6\u8fdf\u89e3\u6790&#xff1a;\u8fd0\u884c\u65f6\u624d\u89e3\u6790\u90e8\u5206\u5e38\u91cf<\/li>\n<\/ul>\n<h4>6.2 \u8bbf\u95ee\u6548\u7387<\/h4>\n<ul>\n<li>\u7d22\u5f15\u8bbf\u95ee&#xff1a;O(1)\u65f6\u95f4\u590d\u6742\u5ea6<\/li>\n<li>\u7f13\u5b58\u53cb\u597d&#xff1a;\u9891\u7e41\u8bbf\u95ee\u7684\u5e38\u91cf\u88ab\u7f13\u5b58<\/li>\n<li>\u9884\u89e3\u6790&#xff1a;\u70ed\u70b9\u5e38\u91cf\u63d0\u524d\u89e3\u6790<\/li>\n<\/ul>\n<h3>\u4e03\u3001\u5e38\u91cf\u6c60\u76f8\u5173\u5f02\u5e38<\/h3>\n<h4>7.1 \u5e38\u89c1\u5f02\u5e38\u7c7b\u578b<\/h4>\n<li>ClassFormatError&#xff1a;\u5e38\u91cf\u6c60\u683c\u5f0f\u9519\u8bef<\/li>\n<li>NoClassDefFoundError&#xff1a;\u5e38\u91cf\u6c60\u5f15\u7528\u7c7b\u4e0d\u5b58\u5728<\/li>\n<li>IncompatibleClassChangeError&#xff1a;\u5e38\u91cf\u6c60\u5f15\u7528\u4e0d\u517c\u5bb9<\/li>\n<h4>7.2 \u793a\u4f8b\u573a\u666f<\/h4>\n<p><span class=\"token comment\">\/\/ \u7f16\u8bd1\u65f6\u5b58\u5728\u4f46\u8fd0\u884c\u65f6\u7f3a\u5931\u7684\u7c7b<\/span><br \/>\n<span class=\"token keyword\">public<\/span> <span class=\"token keyword\">class<\/span> <span class=\"token class-name\">A<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">public<\/span> <span class=\"token keyword\">static<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">String<\/span><span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span> args<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token class-name\">B<\/span> b <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">B<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u82e5B.class\u4e0d\u5b58\u5728<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h3>\u516b\u3001\u5e38\u91cf\u6c60\u5de5\u5177\u4e0e\u8c03\u8bd5<\/h3>\n<h4>8.1 \u67e5\u770b\u5e38\u91cf\u6c60<\/h4>\n<li>\n<p>javap\u547d\u4ee4&#xff1a;<\/p>\n<p> javap -v MyClass.class\n <\/li>\n<li>\n<p>JClassLib\u5de5\u5177&#xff1a;\u56fe\u5f62\u5316\u67e5\u770b\u7c7b\u6587\u4ef6\u7ed3\u6784<\/p>\n<\/li>\n<li>\n<p>ASM\u5e93&#xff1a;\u7f16\u7a0b\u65b9\u5f0f\u8bbf\u95ee\u5e38\u91cf\u6c60<\/p>\n<p> <span class=\"token class-name\">ClassReader<\/span> reader <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">ClassReader<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;MyClass.class&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\nreader<span class=\"token punctuation\">.<\/span><span class=\"token function\">accept<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">new<\/span> <span class=\"token class-name\">ClassVisitor<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">Opcodes<\/span><span class=\"token punctuation\">.<\/span>ASM7<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token annotation punctuation\">&#064;Override<\/span><br \/>\n    <span class=\"token keyword\">public<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">visit<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> version<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">int<\/span> access<span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">String<\/span> name<span class=\"token punctuation\">,<\/span><br \/>\n        <span class=\"token class-name\">String<\/span> signature<span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">String<\/span> superName<span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">String<\/span><span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span> interfaces<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u5904\u7406\u5e38\u91cf\u6c60\u4fe1\u606f<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\n <\/li>\n<h4>8.2 \u6027\u80fd\u5206\u6790<\/h4>\n<li>\n<p>\u5e38\u91cf\u6c60\u5927\u5c0f\u5f71\u54cd&#xff1a;<\/p>\n<p> <span class=\"token comment\"># \u7f16\u8bd1\u65f6\u7edf\u8ba1<\/span><br \/>\njavap -v MyClass <span class=\"token operator\">|<\/span> <span class=\"token function\">grep<\/span> <span class=\"token string\">&#034;const #&#034;<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">wc<\/span> -l\n <\/li>\n<li>\n<p>\u5185\u5b58\u5360\u7528\u5206\u6790&#xff1a;<\/p>\n<p> jmap -dump:format<span class=\"token operator\">&#061;<\/span>b,file<span class=\"token operator\">&#061;<\/span>heap.hprof <span class=\"token operator\">&lt;<\/span>pid<span class=\"token operator\">&gt;<\/span>\n <\/li>\n<h3>\u4e5d\u3001\u5e38\u91cf\u6c60\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<h4>9.1 \u7f16\u7801\u5efa\u8bae<\/h4>\n<li>\n<p>\u590d\u7528\u5e38\u91cf&#xff1a;<\/p>\n<p> <span class=\"token comment\">\/\/ \u63a8\u8350<\/span><br \/>\n<span class=\"token keyword\">public<\/span> <span class=\"token keyword\">static<\/span> <span class=\"token keyword\">final<\/span> <span class=\"token class-name\">String<\/span> DEFAULT_NAME <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;Unknown&#034;<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u4e0d\u63a8\u8350<\/span><br \/>\n<span class=\"token class-name\">String<\/span> name <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">String<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;Unknown&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\n <\/li>\n<li>\n<p>\u8c28\u614e\u4f7f\u7528intern()&#xff1a;<\/p>\n<p> <span class=\"token comment\">\/\/ \u9002\u5408\u91cd\u590d\u4f7f\u7528\u7684\u957f\u5b57\u7b26\u4e32<\/span><br \/>\n<span class=\"token class-name\">String<\/span> key <span class=\"token operator\">&#061;<\/span> largeString<span class=\"token punctuation\">.<\/span><span class=\"token function\">intern<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\n <\/li>\n<li>\n<p>\u679a\u4e3e\u4f18\u4e8e\u5e38\u91cf\u6c60&#xff1a;<\/p>\n<p> <span class=\"token keyword\">enum<\/span> <span class=\"token class-name\">Color<\/span> <span class=\"token punctuation\">{<\/span> RED<span class=\"token punctuation\">,<\/span> GREEN<span class=\"token punctuation\">,<\/span> BLUE <span class=\"token punctuation\">}<\/span> <span class=\"token comment\">\/\/ \u66f4\u7c7b\u578b\u5b89\u5168<\/span>\n <\/li>\n<h4>9.2 \u914d\u7f6e\u8c03\u4f18<\/h4>\n<li>\n<p>\u5b57\u7b26\u4e32\u6c60\u5927\u5c0f&#xff08;JDK7u40&#043;&#xff09;&#xff1a;<\/p>\n<p> -XX:StringTableSize<span class=\"token operator\">&#061;<\/span><span class=\"token number\">60013<\/span>  <span class=\"token comment\"># \u8d28\u6570\u51cf\u5c11\u54c8\u5e0c\u51b2\u7a81<\/span>\n <\/li>\n<li>\n<p>\u5143\u7a7a\u95f4\u914d\u7f6e&#xff08;\u5f71\u54cd\u8fd0\u884c\u65f6\u5e38\u91cf\u6c60&#xff09;&#xff1a;<\/p>\n<p> -XX:MaxMetaspaceSize<span class=\"token operator\">&#061;<\/span>256m\n <\/li>\n<h3>\u5341\u3001\u5e38\u91cf\u6c60\u7684\u5185\u90e8\u5b9e\u73b0&#xff08;HotSpot&#xff09;<\/h3>\n<h4>10.1 \u5b58\u50a8\u7ed3\u6784<\/h4>\n<p>HotSpot\u4f7f\u7528ConstantPool\u548cConstantPoolCache&#xff1a;<\/p>\n<ul>\n<li>ConstantPool&#xff1a;\u5b58\u50a8\u539f\u59cb\u5e38\u91cf\u6570\u636e<\/li>\n<li>ConstantPoolCache&#xff1a;\u7f13\u5b58\u89e3\u6790\u540e\u7684\u76f4\u63a5\u5f15\u7528<\/li>\n<\/ul>\n<p>\/\/ HotSpot\u6e90\u7801\u7247\u6bb5&#xff08;\u7b80\u5316&#xff09;<br \/>\nclass ConstantPool : public MetaspaceObj {<br \/>\n  Array&lt;u1&gt;*           _tags;        \/\/ \u5e38\u91cf\u7c7b\u578b\u6807\u8bb0<br \/>\n  Array&lt;u2&gt;*           _entries;     \/\/ \u5e38\u91cf\u6570\u636e<br \/>\n  ConstantPoolCache*    _cache;      \/\/ \u7f13\u5b58<br \/>\n  \/\/ &#8230;<br \/>\n};<\/p>\n<h4>10.2 \u89e3\u6790\u8fc7\u7a0b<\/h4>\n<li>\n<p>\u7b26\u53f7\u5f15\u7528\u89e3\u6790&#xff1a;<\/p>\n<p> \/\/ \u89e3\u6790\u65b9\u6cd5\u5f15\u7528\u793a\u4f8b<br \/>\nmethodHandle ConstantPool::resolve_method_at(int index) {<br \/>\n  \/\/ \u68c0\u67e5\u7f13\u5b58<br \/>\n  if (_cache-&gt;is_resolved(index)) {<br \/>\n    return _cache-&gt;get_method(index);<br \/>\n  }<br \/>\n  \/\/ \u5b9e\u9645\u89e3\u6790\u903b\u8f91<br \/>\n  return resolve_method_at_impl(index);<br \/>\n}\n <\/li>\n<li>\n<p>\u60f0\u6027\u89e3\u6790&#xff1a;\u90e8\u5206\u5e38\u91cf\u76f4\u5230\u9996\u6b21\u4f7f\u7528\u65f6\u624d\u89e3\u6790<\/p>\n<\/li>\n<h3>\u5341\u4e00\u3001\u5e38\u91cf\u6c60\u4e0eJava\u65b0\u7279\u6027<\/h3>\n<h4>11.1 \u52a8\u6001\u5e38\u91cf&#xff08;JEP 309&#xff09;<\/h4>\n<p>JDK11\u5f15\u5165\u7684\u52a8\u6001\u5e38\u91cf&#xff1a;<\/p>\n<p><span class=\"token comment\">\/\/ \u52a8\u6001\u5e38\u91cf\u793a\u4f8b<\/span><br \/>\n<span class=\"token class-name\">CondyBootstraps<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">constantDynamic<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;name&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">String<\/span><span class=\"token punctuation\">.<\/span><span class=\"token keyword\">class<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;value&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<h4>11.2 \u8bb0\u5f55\u7c7b&#xff08;Record&#xff09;<\/h4>\n<p>\u8bb0\u5f55\u7c7b\u7684\u5e38\u91cf\u6c60\u4f18\u5316&#xff1a;<\/p>\n<p><span class=\"token keyword\">record<\/span> <span class=\"token class-name\">Point<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> x<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">int<\/span> y<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token comment\">\/\/ \u751f\u6210\u66f4\u7b80\u6d01\u7684\u5e38\u91cf\u6c60\u7ed3\u6784<\/span><\/p>\n<h3>\u5341\u4e8c\u3001\u603b\u7ed3<\/h3>\n<p>Java\u5e38\u91cf\u6c60\u7684\u6838\u5fc3\u4ef7\u503c&#xff1a;<\/p>\n<li>\u7a7a\u95f4\u6548\u7387&#xff1a;\u901a\u8fc7\u5171\u4eab\u51cf\u5c11\u5b58\u50a8\u5f00\u9500<\/li>\n<li>\u6267\u884c\u6548\u7387&#xff1a;\u5feb\u901f\u8bbf\u95ee\u7b26\u53f7\u5f15\u7528\u548c\u5e38\u91cf\u503c<\/li>\n<li>\u52a8\u6001\u652f\u6301&#xff1a;\u4e3a\u53cd\u5c04\u3001\u52a8\u6001\u4ee3\u7406\u7b49\u7279\u6027\u63d0\u4f9b\u57fa\u7840<\/li>\n<li>\u8de8\u5e73\u53f0&#xff1a;\u4fdd\u6301\u5b57\u8282\u7801\u4e0e\u5177\u4f53\u5b9e\u73b0\u7684\u89e3\u8026<\/li>\n<p>\u7406\u89e3\u5e38\u91cf\u6c60\u6709\u52a9\u4e8e&#xff1a;<\/p>\n<ul>\n<li>\u8bca\u65ad\u7c7b\u52a0\u8f7d\u95ee\u9898<\/li>\n<li>\u4f18\u5316\u5185\u5b58\u4f7f\u7528<\/li>\n<li>\u7406\u89e3\u5b57\u8282\u7801\u6267\u884c\u673a\u5236<\/li>\n<li>\u8bbe\u8ba1\u66f4\u9ad8\u6548\u7684Java\u4ee3\u7801<\/li>\n<\/ul>\n<p>\u968f\u7740Java\u8bed\u8a00\u7684\u53d1\u5c55&#xff0c;\u5e38\u91cf\u6c60\u7684\u5b9e\u73b0\u4e5f\u5728\u4e0d\u65ad\u6f14\u8fdb&#xff0c;\u4f46\u5176\u4f5c\u4e3a\u8fde\u63a5\u7f16\u8bd1\u65f6\u4e0e\u8fd0\u884c\u65f6\u7684\u6865\u6881\u89d2\u8272\u59cb\u7ec8\u672a\u53d8\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814233940-689e73bce3564.gif\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb931\u6b21\uff0c\u70b9\u8d5e16\u6b21\uff0c\u6536\u85cf22\u6b21\u3002Java\u4e2d\u7684\u5e38\u91cf\u6c60\u6df1\u5ea6\u89e3\u6790<\/p>\n","protected":false},"author":2,"featured_media":57080,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[99,190],"topic":[],"class_list":["post-57082","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-java","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java\u4e2d\u7684\u5e38\u91cf\u6c60\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/57082.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java\u4e2d\u7684\u5e38\u91cf\u6c60\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb931\u6b21\uff0c\u70b9\u8d5e16\u6b21\uff0c\u6536\u85cf22\u6b21\u3002Java\u4e2d\u7684\u5e38\u91cf\u6c60\u6df1\u5ea6\u89e3\u6790\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/57082.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-08-14T23:39:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814233938-689e73ba83273.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/57082.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/57082.html\",\"name\":\"Java\u4e2d\u7684\u5e38\u91cf\u6c60\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-08-14T23:39:42+00:00\",\"dateModified\":\"2025-08-14T23:39:42+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/57082.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/57082.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/57082.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\u4e2d\u7684\u5e38\u91cf\u6c60\u6df1\u5ea6\u89e3\u6790\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java\u4e2d\u7684\u5e38\u91cf\u6c60\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/57082.html","og_locale":"zh_CN","og_type":"article","og_title":"Java\u4e2d\u7684\u5e38\u91cf\u6c60\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb931\u6b21\uff0c\u70b9\u8d5e16\u6b21\uff0c\u6536\u85cf22\u6b21\u3002Java\u4e2d\u7684\u5e38\u91cf\u6c60\u6df1\u5ea6\u89e3\u6790","og_url":"https:\/\/www.wsisp.com\/helps\/57082.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-08-14T23:39:42+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814233938-689e73ba83273.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"5 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/57082.html","url":"https:\/\/www.wsisp.com\/helps\/57082.html","name":"Java\u4e2d\u7684\u5e38\u91cf\u6c60\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-08-14T23:39:42+00:00","dateModified":"2025-08-14T23:39:42+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/57082.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/57082.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/57082.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Java\u4e2d\u7684\u5e38\u91cf\u6c60\u6df1\u5ea6\u89e3\u6790"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/57082","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=57082"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/57082\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/57080"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=57082"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=57082"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=57082"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=57082"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}