\u4f5c\u8005\uff1aGuoMell
\u6765\u6e90\uff1ablog.csdn.net\/gcoder_\/article\/details\/106644312<\/p> \n
0. Background<\/strong><\/h2> \n \u5728 JAVA \u8bed\u8a00\u4e2d\u67098\u4e2d\u57fa\u672c\u7c7b\u578b\u548c\u4e00\u79cd\u6bd4\u8f83\u7279\u6b8a\u7684\u7c7b\u578bString\u3002\u8fd9\u4e9b\u7c7b\u578b\u4e3a\u4e86\u4f7f\u4ed6\u4eec\u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u901f\u5ea6\u66f4\u5feb\uff0c\u66f4\u8282\u7701\u5185\u5b58\uff0c\u90fd\u63d0\u4f9b\u4e86\u4e00\u79cd\u5e38\u91cf\u6c60\u7684\u6982\u5ff5\u3002\u5e38\u91cf\u6c60\u5c31\u7c7b\u4f3c\u4e00\u4e2aJAVA\u7cfb\u7edf\u7ea7\u522b\u63d0\u4f9b\u7684\u7f13\u5b58\u3002<\/p> \n
8\u79cd\u57fa\u672c\u7c7b\u578b\u7684\u5e38\u91cf\u6c60\u90fd\u662f\u7cfb\u7edf\u534f\u8c03\u7684\uff0cString\u7c7b\u578b\u7684\u5e38\u91cf\u6c60\u6bd4\u8f83\u7279\u6b8a\u3002<\/p> \n
\u5b83\u7684\u4e3b\u8981\u4f7f\u7528\u65b9\u6cd5\u6709\u4e24\u79cd\uff1a<\/p> \n
\n - \u76f4\u63a5\u4f7f\u7528\u53cc\u5f15\u53f7\u58f0\u660e\u51fa\u6765\u7684String\u5bf9\u8c61\u4f1a\u76f4\u63a5\u5b58\u50a8\u5728\u5e38\u91cf\u6c60\u4e2d\u3002<\/li> \n
- \u5982\u679c\u4e0d\u662f\u7528\u53cc\u5f15\u53f7\u58f0\u660e\u7684String\u5bf9\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528String\u63d0\u4f9b\u7684intern\u65b9\u6cd5\u3002intern \u65b9\u6cd5\u4f1a\u4ece\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\u4e2d\u67e5\u8be2\u5f53\u524d\u5b57\u7b26\u4e32\u662f\u5426\u5b58\u5728\uff0c\u82e5\u4e0d\u5b58\u5728\u5c31\u4f1a\u5c06\u5f53\u524d\u5b57\u7b26\u4e32\u653e\u5165\u5e38\u91cf\u6c60\u4e2d<\/li> \n <\/ul> \n
\u63a8\u8350\u4e00\u4e2a\u5f00\u6e90\u514d\u8d39\u7684 Spring Boot \u5b9e\u6218\u9879\u76ee\uff1a<\/p> \n
\n https:\/\/github.com\/java<\/a>stacks\/spring-boot-best-practice<\/a><\/p> \n <\/blockquote> \n 1. \u5e38\u91cf\u6c60<\/strong><\/h2> \n 1.1 \u5e38\u91cf\u6c60\u662f\u4ec0\u4e48\uff1f<\/h3> \n
<\/p> \n
JVM\u5e38\u91cf\u6c60\u4e3b\u8981\u5206\u4e3aClass\u6587\u4ef6\u5e38\u91cf\u6c60\u3001\u8fd0\u884c\u65f6\u5e38\u91cf\u6c60\uff0c\u5168\u5c40\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\uff0c\u4ee5\u53ca\u57fa\u672c\u7c7b\u578b\u5305\u88c5\u7c7b\u5bf9\u8c61\u5e38\u91cf\u6c60<\/p> \n
1.1.0 \u65b9\u6cd5\u533a<\/h4> \n
\u65b9\u6cd5\u533a\u7684\u4f5c\u7528\u662f\u5b58\u50a8Java\u7c7b\u7684\u7ed3\u6784\u4fe1\u606f\uff0c\u5f53\u521b\u5efa\u5bf9\u8c61\u540e\uff0c\u5bf9\u8c61\u7684\u7c7b\u578b\u4fe1\u606f\u5b58\u50a8\u5728\u65b9\u6cd5\u533a\u4e2d\uff0c\u5b9e\u4f8b\u6570\u636e\u5b58\u653e\u5728\u5806\u4e2d\u3002\u7c7b\u578b\u4fe1\u606f\u662f\u5b9a\u4e49\u5728Java\u4ee3\u7801\u4e2d\u7684\u5e38\u91cf\u3001\u9759\u6001\u53d8\u91cf\u3001\u4ee5\u53ca\u7c7b\u4e2d\u58f0\u660e\u7684\u5404\u79cd\u65b9\u6cd5\uff0c\u65b9\u6cd5\u5b57\u6bb5\u7b49\uff1b\u5b9e\u4f8b\u6570\u636e\u5219\u662f\u5728Java\u4e2d\u521b\u5efa\u7684\u5bf9\u8c61\u5b9e\u4f8b\u4ee5\u53ca\u4ed6\u4eec\u7684\u503c\u3002<\/p> \n
\u8be5\u533a\u57df\u8fdb\u884c\u5185\u5b58\u56de\u6536\u7684\u4e3b\u8981\u76ee\u7684\u662f\u5bf9\u5e38\u91cf\u6c60\u7684\u56de\u6536\u548c\u5bf9\u5185\u5b58\u6570\u636e\u7684\u5378\u8f7d\uff1b\u4e00\u822c\u8bf4\u8fd9\u4e2a\u533a\u57df\u7684\u5185\u5b58\u56de\u6536\u7387\u6bd4\u8d77Java\u5806\u4f4e\u5f97\u591a\u3002<\/p> \n
1.1.1 Class\u6587\u4ef6\u5e38\u91cf\u6c60<\/h4> \n
class\u6587\u4ef6\u662f\u4e00\u7ec4\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u6d41\uff0c\u5728Java\u4ee3\u7801\u7684\u7f16\u8bd1\u671f\u95f4\uff0c\u6211\u4eec\u7f16\u5199\u7684Java\u6587\u4ef6\u5c31\u88ab\u7f16\u8bd1\u4e3a.class\u6587\u4ef6\u683c\u5f0f\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u5b58\u653e\u5728\u78c1\u76d8\u4e2d\uff0c\u5176\u4e2d\u5c31\u5305\u62ecclass\u6587\u4ef6\u5e38\u91cf\u6c60\u3002<\/p> \n
class\u6587\u4ef6\u5e38\u91cf\u6c60\u4e3b\u8981\u5b58\u653e\u4e24\u5927\u5e38\u91cf\uff1a\u5b57\u9762\u91cf\u548c\u7b26\u53f7\u5f15\u7528<\/strong>\u3002<\/p> \n \u5b57\u9762\u91cf\uff1a\u5b57\u9762\u91cf\u63a5\u8fd1java<\/a>\u8bed\u8a00\u5c42\u9762\u7684\u5e38\u91cf\u6982\u5ff5<\/p> \n \n - \u6587\u672c\u5b57\u7b26\u4e32\uff0c\u4e5f\u5c31\u662f\u6211\u4eec\u7ecf\u5e38\u7533\u660e\u7684\uff1apublic String s = "abc";\u4e2d\u7684"abc"<\/li> \n
- \u7528final\u4fee\u9970\u7684\u6210\u5458\u53d8\u91cf\uff0c\u5305\u62ec\u9759\u6001\u53d8\u91cf\u3001\u5b9e\u4f8b\u53d8\u91cf\u548c\u5c40\u90e8\u53d8\u91cf\uff1apublic final static int f = 0x101;,final int temp = 3;<\/li> \n
- \u800c\u5bf9\u4e8e\u57fa\u672c\u7c7b\u578b\u6570\u636e(\u751a\u81f3\u662f\u65b9\u6cd5\u4e2d\u7684\u5c40\u90e8\u53d8\u91cf)\uff0c\u5982int value = 1\u5e38\u91cf\u6c60\u4e2d\u53ea\u4fdd\u7559\u4e86\u4ed6\u7684\u7684\u5b57\u6bb5\u63cf\u8ff0\u7b26int\u548c\u5b57\u6bb5\u7684\u540d\u79f0value\uff0c\u4ed6\u4eec\u7684\u5b57\u9762\u91cf\u4e0d\u4f1a\u5b58\u5728\u4e8e\u5e38\u91cf\u6c60\u3002<\/li> \n <\/ul> \n
\u7b26\u53f7\u5f15\u7528\uff1a\u7b26\u53f7\u5f15\u7528\u4e3b\u8981\u8bbe\u6d89\u53ca\u7f16\u8bd1\u539f\u7406\u65b9\u9762\u7684\u6982\u5ff5<\/p> \n
\n - \u7c7b\u548c\u63a5\u53e3\u7684\u5168\u9650\u5b9a\u540d\uff0c\u4e5f\u5c31\u662fjava\/lang\/String;\u8fd9\u6837\uff0c\u5c06\u7c7b\u540d\u4e2d\u539f\u6765\u7684".\u201c\u66ff\u6362\u4e3a\u201d\/"\u5f97\u5230\u7684\uff0c\u4e3b\u8981\u7528\u4e8e\u5728\u8fd0\u884c\u65f6\u89e3\u6790\u5f97\u5230\u7c7b\u7684\u76f4\u63a5\u5f15\u7528<\/li> \n
- \u5b57\u6bb5\u7684\u540d\u79f0\u548c\u63cf\u8ff0\u7b26\uff0c\u5b57\u6bb5\u4e5f\u5c31\u662f\u7c7b\u6216\u8005\u63a5\u53e3\u4e2d\u58f0\u660e\u7684\u53d8\u91cf\uff0c\u5305\u62ec\u7c7b\u7ea7\u522b\u53d8\u91cf\u548c\u5b9e\u4f8b\u7ea7\u7684\u53d8\u91cf<\/li> \n
- \u65b9\u6cd5\u4e2d\u7684\u540d\u79f0\u548c\u63cf\u8ff0\u7b26\uff0c\u4e5f\u5373\u53c2\u6570\u7c7b\u578b+\u8fd4\u56de\u503c<\/li> \n <\/ul> \n
1.1.2 \u8fd0\u884c\u65f6\u5e38\u91cf\u6c60<\/h4> \n
\u5f53Java\u6587\u4ef6\u88ab\u7f16\u8bd1\u6210class\u6587\u4ef6\u4e4b\u540e\uff0c\u4f1a\u751f\u6210\u4e0a\u9762\u7684class\u6587\u4ef6\u5e38\u91cf\u6c60\uff0cJVM\u5728\u6267\u884c\u67d0\u4e2a\u7c7b\u7684\u65f6\u5019\uff0c\u5fc5\u987b\u7ecf\u8fc7\u52a0\u8f7d\u3001\u94fe\u63a5\uff08\u9a8c\u8bc1\u3001\u51c6\u5907\u3001\u89e3\u6790\uff09\u3001\u521d\u59cb\u5316\u7684\u6b65\u9139\uff0c\u8fd0\u884c\u65f6\u5e38\u91cf\u6c60\u5219\u662f\u5728JVM\u5c06\u7c7b\u52a0\u8f7d\u5230\u5185\u5b58\u540e\uff0c\u5c31\u4f1a\u5c06class\u5e38\u91cf\u6c60\u4e2d\u7684\u5185\u5bb9\u5b58\u653e\u5230\u8fd0\u884c\u65f6\u5e38\u91cf\u6c60\u4e2d\uff0c\u4e5f\u5c31\u662fclass\u5e38\u91cf\u6c60\u88ab\u52a0\u8f7d\u5230\u5185\u5b58\u4e4b\u540e\u7684\u7248\u672c\uff0c\u662f\u65b9\u6cd5\u533a\u7684\u4e00\u90e8\u5206\u3002<\/p> \n
\u5728\u89e3\u6790\u9636\u6bb5\uff0c\u4f1a\u628a\u7b26\u53f7\u5f15\u7528\u66ff\u6362\u4e3a\u76f4\u63a5\u5f15\u7528\uff0c\u89e3\u6790\u7684\u8fc7\u7a0b\u4f1a\u53bb\u67e5\u8be2\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\uff0c\u4e5f\u5c31StringTable\uff0c\u4ee5\u4fdd\u8bc1\u8fd0\u884c\u65f6\u5e38\u91cf\u6c60\u6240\u5f15\u7528\u7684\u5b57\u7b26\u4e32\u4e0e\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\u4e2d\u662f\u4e00\u81f4\u7684\u3002<\/p> \n
\u8fd0\u884c\u65f6\u5e38\u91cf\u6c60\u76f8\u5bf9\u4e8eclass\u5e38\u91cf\u6c60\u4e00\u5927\u7279\u5f81\u5c31\u662f\u5177\u6709\u52a8\u6001\u6027\uff0cJava\u89c4\u8303\u5e76\u4e0d\u8981\u6c42\u5e38\u91cf\u53ea\u80fd\u5728\u8fd0\u884c\u65f6\u624d\u4ea7\u751f\uff0c\u4e5f\u5c31\u662f\u8bf4\u8fd0\u884c\u65f6\u5e38\u91cf\u6c60\u7684\u5185\u5bb9\u5e76\u4e0d\u5168\u90e8\u6765\u81eaclass\u5e38\u91cf\u6c60\uff0c\u5728\u8fd0\u884c\u65f6\u53ef\u4ee5\u901a\u8fc7\u4ee3\u7801\u751f\u6210\u5e38\u91cf\u5e76\u5c06\u5176\u653e\u5165\u8fd0\u884c\u65f6\u5e38\u91cf\u6c60\u4e2d\uff0c\u8fd9\u79cd\u7279\u6027\u88ab\u7528\u7684\u6700\u591a\u7684\u5c31\u662fString.intern()\u3002<\/p> \n
1.1.3 \u5b57\u7b26\u4e32\u5e38\u91cf\u6c60<\/h4> \n
\u5728JDK6.0\u53ca\u4e4b\u524d\u7248\u672c\uff0c\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\u5b58\u653e\u5728\u65b9\u6cd5\u533a\u4e2d\uff0c\u5728JDK7.0\u7248\u672c\u4ee5\u540e\uff0c\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\u88ab\u79fb\u5230\u4e86\u5806\u4e2d\u4e86\u3002\u81f3\u4e8e\u4e3a\u4ec0\u4e48\u79fb\u5230\u5806\u5185\uff0c\u5927\u6982\u662f\u7531\u4e8e\u65b9\u6cd5\u533a\u7684\u5185\u5b58\u7a7a\u95f4\u592a\u5c0f\u4e86\u3002\u5728HotSpot VM\u91cc\u5b9e\u73b0\u7684string pool\u529f\u80fd\u7684\u662f\u4e00\u4e2aStringTable\u7c7b\uff0c\u5b83\u662f\u4e00\u4e2aHash\u8868\uff0c\u9ed8\u8ba4\u503c\u5927\u5c0f\u957f\u5ea6\u662f1009\uff1b\u8fd9\u4e2aStringTable\u5728\u6bcf\u4e2aHotSpot VM\u7684\u5b9e\u4f8b\u53ea\u6709\u4e00\u4efd\uff0c\u88ab\u6240\u6709\u7684\u7c7b\u5171\u4eab\u3002\u5b57\u7b26\u4e32\u5e38\u91cf\u7531\u4e00\u4e2a\u4e00\u4e2a\u5b57\u7b26\u7ec4\u6210\uff0c\u653e\u5728\u4e86StringTable\u4e0a\u3002<\/p> \n
\u5728JDK6.0\u4e2d\uff0cStringTable\u7684\u957f\u5ea6\u662f\u56fa\u5b9a\u7684\uff0c\u957f\u5ea6\u5c31\u662f1009\uff0c\u56e0\u6b64\u5982\u679c\u653e\u5165String Pool\u4e2d\u7684String\u975e\u5e38\u591a\uff0c\u5c31\u4f1a\u9020\u6210hash\u51b2\u7a81\uff0c\u5bfc\u81f4\u94fe\u8868\u8fc7\u957f\uff0c\u5f53\u8c03\u7528String#intern()\u65f6\u4f1a\u9700\u8981\u5230\u94fe\u8868\u4e0a\u4e00\u4e2a\u4e00\u4e2a\u627e\uff0c\u4ece\u800c\u5bfc\u81f4\u6027\u80fd\u5927\u5e45\u5ea6\u4e0b\u964d\uff1b\u5728JDK7.0\u4e2d\uff0cStringTable\u7684\u957f\u5ea6\u53ef\u4ee5\u901a\u8fc7\u53c2\u6570\u6307\u5b9a\u3002<\/p> \n
\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\u8bbe\u8ba1\u601d\u60f3\uff1a<\/p> \n
\n -
\u5b57\u7b26\u4e32\u7684\u5206\u914d\uff0c\u548c\u5176\u4ed6\u7684\u5bf9\u8c61\u5206\u914d\u4e00\u6837\uff0c\u8017\u8d39\u9ad8\u6602\u7684\u65f6\u95f4\u4e0e\u7a7a\u95f4\u4ee3\u4ef7\uff0c\u4f5c\u4e3a\u6700\u57fa\u7840\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5927\u91cf\u9891\u7e41\u7684\u521b\u5efa\u5b57\u7b26\u4e32\uff0c\u6781\u5927\u7a0b\u5ea6\u5730\u5f71\u54cd\u7a0b\u5e8f\u7684\u6027\u80fd<\/p> <\/li> \n
-
JVM\u4e3a\u4e86\u63d0\u9ad8\u6027\u80fd\u548c\u51cf\u5c11\u5185\u5b58\u5f00\u9500\uff0c\u5728\u5b9e\u4f8b\u5316\u5b57\u7b26\u4e32\u5e38\u91cf\u7684\u65f6\u5019\u8fdb\u884c\u4e86\u4e00\u4e9b\u4f18\u5316<\/p> <\/li> \n
- \n
\n - \u4e3a\u5b57\u7b26\u4e32\u5f00\u8f9f\u4e00\u4e2a\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\uff0c\u7c7b\u4f3c\u4e8e\u7f13\u5b58\u533a<\/li> \n
- \u521b\u5efa\u5b57\u7b26\u4e32\u5e38\u91cf\u65f6\uff0c\u9996\u5148\u67e5\u770b\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\u662f\u5426\u5b58\u5728\u8be5\u5b57\u7b26\u4e32<\/li> \n
- \u5b58\u5728\u8be5\u5b57\u7b26\u4e32\uff0c\u8fd4\u56de\u5f15\u7528\u5b9e\u4f8b\uff0c\u4e0d\u5b58\u5728\uff0c\u5b9e\u4f8b\u5316\u8be5\u5b57\u7b26\u4e32\u5e76\u653e\u5165\u6c60\u4e2d<\/li> \n <\/ul> <\/li> \n
-
\u5b9e\u73b0\u7684\u57fa\u7840<\/p> <\/li> \n
- \n
\n - \u5b9e\u73b0\u8be5\u4f18\u5316\u7684\u57fa\u7840\u662f\u56e0\u4e3a\u5b57\u7b26\u4e32\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u53ef\u4ee5\u4e0d\u7528\u62c5\u5fc3\u6570\u636e\u51b2\u7a81\u8fdb\u884c\u5171\u4eab<\/li> \n
- \u8fd0\u884c\u65f6\u5b9e\u4f8b\u521b\u5efa\u7684\u5168\u5c40\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\u4e2d\u6709\u4e00\u4e2a\u8868\uff0c\u603b\u662f\u4e3a\u6c60\u4e2d\u6bcf\u4e2a\u552f\u4e00\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61\u7ef4\u62a4\u4e00\u4e2a\u5f15\u7528,\u8fd9\u5c31\u610f\u5473\u7740\u5b83\u4eec\u4e00\u76f4\u5f15\u7528\u7740\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\u4e2d\u7684\u5bf9\u8c61\uff0c\u6240\u4ee5\uff0c\u5728\u5e38\u91cf\u6c60\u4e2d\u7684\u8fd9\u4e9b\u5b57\u7b26\u4e32\u4e0d\u4f1a\u88ab\u5783\u573e\u6536\u96c6\u5668\u56de\u6536<\/li> \n <\/ul> <\/li> \n <\/ul> \n
2. String.intern()\u4e0e\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60<\/strong><\/h2> \n \/**\n * Returns a canonical representation for the string object.\n * <p>\n * A pool of strings, initially empty, is maintained privately by the\n * class <code>String<\/code>.\n * <p>\n * When the intern method is invoked, if the pool already contains a\n * string equal to this <code>String<\/code> object as determined by\n * the {@link #equals(Object)} method, then the string from the pool is\n * returned. Otherwise, this <code>String<\/code> object is added to the\n * pool and a reference to this <code>String<\/code> object is returned.\n * <p>\n * It follows that for any two strings <code>s<\/code> and <code>t<\/code>,\n * <code>s.in","orderid":"0","title":"\u9762\u8bd5\u5b98\uff1aString.intern() \u6709\u4ec0\u4e48\u7528\uff1f\u548c\u5e38\u91cf\u6c60\u6709\u4ec0\u4e48\u5173\u7cfb\uff1f\u95ee\u5012\u4e00\u5927\u7247\uff01(\u4e00)","smalltitle":"","mid":"0","fname":"JAVA","special_id":"0","bak_id":"0","info":"0","hits":"57","pages":"3","comments":"0","posttime":"2023-08-26 21:10:55","list":"1693055455","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"String.intern<\/A> \u5e38\u91cf\u6c60<\/A> \u5173\u7cfb<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"220.152.140.35","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"\u9762\u8bd5\u5b98\uff1aString.intern() \u6709\u4ec0\u4e48\u7528\uff1f\u548c\u5e38\u91cf\u6c60\u6709\u4ec0\u4e48\u5173\u7cfb\uff1f\u95ee\u5012\u4e00\u5927\u7247\uff01","lastview":"1713198157","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}
\u63a8\u8350\u4e00\u4e2a\u5f00\u6e90\u514d\u8d39\u7684 Spring Boot \u5b9e\u6218\u9879\u76ee\uff1a<\/p> \n
\nhttps:\/\/github.com\/java<\/a>stacks\/spring-boot-best-practice<\/a><\/p> \n <\/blockquote> \n
1. \u5e38\u91cf\u6c60<\/strong><\/h2> \n
1.1 \u5e38\u91cf\u6c60\u662f\u4ec0\u4e48\uff1f<\/h3> \n
<\/p> \n
JVM\u5e38\u91cf\u6c60\u4e3b\u8981\u5206\u4e3aClass\u6587\u4ef6\u5e38\u91cf\u6c60\u3001\u8fd0\u884c\u65f6\u5e38\u91cf\u6c60\uff0c\u5168\u5c40\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\uff0c\u4ee5\u53ca\u57fa\u672c\u7c7b\u578b\u5305\u88c5\u7c7b\u5bf9\u8c61\u5e38\u91cf\u6c60<\/p> \n
1.1.0 \u65b9\u6cd5\u533a<\/h4> \n
\u65b9\u6cd5\u533a\u7684\u4f5c\u7528\u662f\u5b58\u50a8Java\u7c7b\u7684\u7ed3\u6784\u4fe1\u606f\uff0c\u5f53\u521b\u5efa\u5bf9\u8c61\u540e\uff0c\u5bf9\u8c61\u7684\u7c7b\u578b\u4fe1\u606f\u5b58\u50a8\u5728\u65b9\u6cd5\u533a\u4e2d\uff0c\u5b9e\u4f8b\u6570\u636e\u5b58\u653e\u5728\u5806\u4e2d\u3002\u7c7b\u578b\u4fe1\u606f\u662f\u5b9a\u4e49\u5728Java\u4ee3\u7801\u4e2d\u7684\u5e38\u91cf\u3001\u9759\u6001\u53d8\u91cf\u3001\u4ee5\u53ca\u7c7b\u4e2d\u58f0\u660e\u7684\u5404\u79cd\u65b9\u6cd5\uff0c\u65b9\u6cd5\u5b57\u6bb5\u7b49\uff1b\u5b9e\u4f8b\u6570\u636e\u5219\u662f\u5728Java\u4e2d\u521b\u5efa\u7684\u5bf9\u8c61\u5b9e\u4f8b\u4ee5\u53ca\u4ed6\u4eec\u7684\u503c\u3002<\/p> \n
\u8be5\u533a\u57df\u8fdb\u884c\u5185\u5b58\u56de\u6536\u7684\u4e3b\u8981\u76ee\u7684\u662f\u5bf9\u5e38\u91cf\u6c60\u7684\u56de\u6536\u548c\u5bf9\u5185\u5b58\u6570\u636e\u7684\u5378\u8f7d\uff1b\u4e00\u822c\u8bf4\u8fd9\u4e2a\u533a\u57df\u7684\u5185\u5b58\u56de\u6536\u7387\u6bd4\u8d77Java\u5806\u4f4e\u5f97\u591a\u3002<\/p> \n
1.1.1 Class\u6587\u4ef6\u5e38\u91cf\u6c60<\/h4> \n
class\u6587\u4ef6\u662f\u4e00\u7ec4\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u6d41\uff0c\u5728Java\u4ee3\u7801\u7684\u7f16\u8bd1\u671f\u95f4\uff0c\u6211\u4eec\u7f16\u5199\u7684Java\u6587\u4ef6\u5c31\u88ab\u7f16\u8bd1\u4e3a.class\u6587\u4ef6\u683c\u5f0f\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u5b58\u653e\u5728\u78c1\u76d8\u4e2d\uff0c\u5176\u4e2d\u5c31\u5305\u62ecclass\u6587\u4ef6\u5e38\u91cf\u6c60\u3002<\/p> \n
class\u6587\u4ef6\u5e38\u91cf\u6c60\u4e3b\u8981\u5b58\u653e\u4e24\u5927\u5e38\u91cf\uff1a\u5b57\u9762\u91cf\u548c\u7b26\u53f7\u5f15\u7528<\/strong>\u3002<\/p> \n
\u5b57\u9762\u91cf\uff1a\u5b57\u9762\u91cf\u63a5\u8fd1java<\/a>\u8bed\u8a00\u5c42\u9762\u7684\u5e38\u91cf\u6982\u5ff5<\/p> \n
\n
- \u6587\u672c\u5b57\u7b26\u4e32\uff0c\u4e5f\u5c31\u662f\u6211\u4eec\u7ecf\u5e38\u7533\u660e\u7684\uff1apublic String s = "abc";\u4e2d\u7684"abc"<\/li> \n
- \u7528final\u4fee\u9970\u7684\u6210\u5458\u53d8\u91cf\uff0c\u5305\u62ec\u9759\u6001\u53d8\u91cf\u3001\u5b9e\u4f8b\u53d8\u91cf\u548c\u5c40\u90e8\u53d8\u91cf\uff1apublic final static int f = 0x101;,final int temp = 3;<\/li> \n
- \u800c\u5bf9\u4e8e\u57fa\u672c\u7c7b\u578b\u6570\u636e(\u751a\u81f3\u662f\u65b9\u6cd5\u4e2d\u7684\u5c40\u90e8\u53d8\u91cf)\uff0c\u5982int value = 1\u5e38\u91cf\u6c60\u4e2d\u53ea\u4fdd\u7559\u4e86\u4ed6\u7684\u7684\u5b57\u6bb5\u63cf\u8ff0\u7b26int\u548c\u5b57\u6bb5\u7684\u540d\u79f0value\uff0c\u4ed6\u4eec\u7684\u5b57\u9762\u91cf\u4e0d\u4f1a\u5b58\u5728\u4e8e\u5e38\u91cf\u6c60\u3002<\/li> \n <\/ul> \n
\u7b26\u53f7\u5f15\u7528\uff1a\u7b26\u53f7\u5f15\u7528\u4e3b\u8981\u8bbe\u6d89\u53ca\u7f16\u8bd1\u539f\u7406\u65b9\u9762\u7684\u6982\u5ff5<\/p> \n
\n
- \u7c7b\u548c\u63a5\u53e3\u7684\u5168\u9650\u5b9a\u540d\uff0c\u4e5f\u5c31\u662fjava\/lang\/String;\u8fd9\u6837\uff0c\u5c06\u7c7b\u540d\u4e2d\u539f\u6765\u7684".\u201c\u66ff\u6362\u4e3a\u201d\/"\u5f97\u5230\u7684\uff0c\u4e3b\u8981\u7528\u4e8e\u5728\u8fd0\u884c\u65f6\u89e3\u6790\u5f97\u5230\u7c7b\u7684\u76f4\u63a5\u5f15\u7528<\/li> \n
- \u5b57\u6bb5\u7684\u540d\u79f0\u548c\u63cf\u8ff0\u7b26\uff0c\u5b57\u6bb5\u4e5f\u5c31\u662f\u7c7b\u6216\u8005\u63a5\u53e3\u4e2d\u58f0\u660e\u7684\u53d8\u91cf\uff0c\u5305\u62ec\u7c7b\u7ea7\u522b\u53d8\u91cf\u548c\u5b9e\u4f8b\u7ea7\u7684\u53d8\u91cf<\/li> \n
- \u65b9\u6cd5\u4e2d\u7684\u540d\u79f0\u548c\u63cf\u8ff0\u7b26\uff0c\u4e5f\u5373\u53c2\u6570\u7c7b\u578b+\u8fd4\u56de\u503c<\/li> \n <\/ul> \n
1.1.2 \u8fd0\u884c\u65f6\u5e38\u91cf\u6c60<\/h4> \n
\u5f53Java\u6587\u4ef6\u88ab\u7f16\u8bd1\u6210class\u6587\u4ef6\u4e4b\u540e\uff0c\u4f1a\u751f\u6210\u4e0a\u9762\u7684class\u6587\u4ef6\u5e38\u91cf\u6c60\uff0cJVM\u5728\u6267\u884c\u67d0\u4e2a\u7c7b\u7684\u65f6\u5019\uff0c\u5fc5\u987b\u7ecf\u8fc7\u52a0\u8f7d\u3001\u94fe\u63a5\uff08\u9a8c\u8bc1\u3001\u51c6\u5907\u3001\u89e3\u6790\uff09\u3001\u521d\u59cb\u5316\u7684\u6b65\u9139\uff0c\u8fd0\u884c\u65f6\u5e38\u91cf\u6c60\u5219\u662f\u5728JVM\u5c06\u7c7b\u52a0\u8f7d\u5230\u5185\u5b58\u540e\uff0c\u5c31\u4f1a\u5c06class\u5e38\u91cf\u6c60\u4e2d\u7684\u5185\u5bb9\u5b58\u653e\u5230\u8fd0\u884c\u65f6\u5e38\u91cf\u6c60\u4e2d\uff0c\u4e5f\u5c31\u662fclass\u5e38\u91cf\u6c60\u88ab\u52a0\u8f7d\u5230\u5185\u5b58\u4e4b\u540e\u7684\u7248\u672c\uff0c\u662f\u65b9\u6cd5\u533a\u7684\u4e00\u90e8\u5206\u3002<\/p> \n
\u5728\u89e3\u6790\u9636\u6bb5\uff0c\u4f1a\u628a\u7b26\u53f7\u5f15\u7528\u66ff\u6362\u4e3a\u76f4\u63a5\u5f15\u7528\uff0c\u89e3\u6790\u7684\u8fc7\u7a0b\u4f1a\u53bb\u67e5\u8be2\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\uff0c\u4e5f\u5c31StringTable\uff0c\u4ee5\u4fdd\u8bc1\u8fd0\u884c\u65f6\u5e38\u91cf\u6c60\u6240\u5f15\u7528\u7684\u5b57\u7b26\u4e32\u4e0e\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\u4e2d\u662f\u4e00\u81f4\u7684\u3002<\/p> \n
\u8fd0\u884c\u65f6\u5e38\u91cf\u6c60\u76f8\u5bf9\u4e8eclass\u5e38\u91cf\u6c60\u4e00\u5927\u7279\u5f81\u5c31\u662f\u5177\u6709\u52a8\u6001\u6027\uff0cJava\u89c4\u8303\u5e76\u4e0d\u8981\u6c42\u5e38\u91cf\u53ea\u80fd\u5728\u8fd0\u884c\u65f6\u624d\u4ea7\u751f\uff0c\u4e5f\u5c31\u662f\u8bf4\u8fd0\u884c\u65f6\u5e38\u91cf\u6c60\u7684\u5185\u5bb9\u5e76\u4e0d\u5168\u90e8\u6765\u81eaclass\u5e38\u91cf\u6c60\uff0c\u5728\u8fd0\u884c\u65f6\u53ef\u4ee5\u901a\u8fc7\u4ee3\u7801\u751f\u6210\u5e38\u91cf\u5e76\u5c06\u5176\u653e\u5165\u8fd0\u884c\u65f6\u5e38\u91cf\u6c60\u4e2d\uff0c\u8fd9\u79cd\u7279\u6027\u88ab\u7528\u7684\u6700\u591a\u7684\u5c31\u662fString.intern()\u3002<\/p> \n
1.1.3 \u5b57\u7b26\u4e32\u5e38\u91cf\u6c60<\/h4> \n
\u5728JDK6.0\u53ca\u4e4b\u524d\u7248\u672c\uff0c\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\u5b58\u653e\u5728\u65b9\u6cd5\u533a\u4e2d\uff0c\u5728JDK7.0\u7248\u672c\u4ee5\u540e\uff0c\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\u88ab\u79fb\u5230\u4e86\u5806\u4e2d\u4e86\u3002\u81f3\u4e8e\u4e3a\u4ec0\u4e48\u79fb\u5230\u5806\u5185\uff0c\u5927\u6982\u662f\u7531\u4e8e\u65b9\u6cd5\u533a\u7684\u5185\u5b58\u7a7a\u95f4\u592a\u5c0f\u4e86\u3002\u5728HotSpot VM\u91cc\u5b9e\u73b0\u7684string pool\u529f\u80fd\u7684\u662f\u4e00\u4e2aStringTable\u7c7b\uff0c\u5b83\u662f\u4e00\u4e2aHash\u8868\uff0c\u9ed8\u8ba4\u503c\u5927\u5c0f\u957f\u5ea6\u662f1009\uff1b\u8fd9\u4e2aStringTable\u5728\u6bcf\u4e2aHotSpot VM\u7684\u5b9e\u4f8b\u53ea\u6709\u4e00\u4efd\uff0c\u88ab\u6240\u6709\u7684\u7c7b\u5171\u4eab\u3002\u5b57\u7b26\u4e32\u5e38\u91cf\u7531\u4e00\u4e2a\u4e00\u4e2a\u5b57\u7b26\u7ec4\u6210\uff0c\u653e\u5728\u4e86StringTable\u4e0a\u3002<\/p> \n
\u5728JDK6.0\u4e2d\uff0cStringTable\u7684\u957f\u5ea6\u662f\u56fa\u5b9a\u7684\uff0c\u957f\u5ea6\u5c31\u662f1009\uff0c\u56e0\u6b64\u5982\u679c\u653e\u5165String Pool\u4e2d\u7684String\u975e\u5e38\u591a\uff0c\u5c31\u4f1a\u9020\u6210hash\u51b2\u7a81\uff0c\u5bfc\u81f4\u94fe\u8868\u8fc7\u957f\uff0c\u5f53\u8c03\u7528String#intern()\u65f6\u4f1a\u9700\u8981\u5230\u94fe\u8868\u4e0a\u4e00\u4e2a\u4e00\u4e2a\u627e\uff0c\u4ece\u800c\u5bfc\u81f4\u6027\u80fd\u5927\u5e45\u5ea6\u4e0b\u964d\uff1b\u5728JDK7.0\u4e2d\uff0cStringTable\u7684\u957f\u5ea6\u53ef\u4ee5\u901a\u8fc7\u53c2\u6570\u6307\u5b9a\u3002<\/p> \n
\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\u8bbe\u8ba1\u601d\u60f3\uff1a<\/p> \n
\n
\u5b57\u7b26\u4e32\u7684\u5206\u914d\uff0c\u548c\u5176\u4ed6\u7684\u5bf9\u8c61\u5206\u914d\u4e00\u6837\uff0c\u8017\u8d39\u9ad8\u6602\u7684\u65f6\u95f4\u4e0e\u7a7a\u95f4\u4ee3\u4ef7\uff0c\u4f5c\u4e3a\u6700\u57fa\u7840\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5927\u91cf\u9891\u7e41\u7684\u521b\u5efa\u5b57\u7b26\u4e32\uff0c\u6781\u5927\u7a0b\u5ea6\u5730\u5f71\u54cd\u7a0b\u5e8f\u7684\u6027\u80fd<\/p> <\/li> \n
JVM\u4e3a\u4e86\u63d0\u9ad8\u6027\u80fd\u548c\u51cf\u5c11\u5185\u5b58\u5f00\u9500\uff0c\u5728\u5b9e\u4f8b\u5316\u5b57\u7b26\u4e32\u5e38\u91cf\u7684\u65f6\u5019\u8fdb\u884c\u4e86\u4e00\u4e9b\u4f18\u5316<\/p> <\/li> \n
- \n
\n
- \u4e3a\u5b57\u7b26\u4e32\u5f00\u8f9f\u4e00\u4e2a\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\uff0c\u7c7b\u4f3c\u4e8e\u7f13\u5b58\u533a<\/li> \n
- \u521b\u5efa\u5b57\u7b26\u4e32\u5e38\u91cf\u65f6\uff0c\u9996\u5148\u67e5\u770b\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\u662f\u5426\u5b58\u5728\u8be5\u5b57\u7b26\u4e32<\/li> \n
- \u5b58\u5728\u8be5\u5b57\u7b26\u4e32\uff0c\u8fd4\u56de\u5f15\u7528\u5b9e\u4f8b\uff0c\u4e0d\u5b58\u5728\uff0c\u5b9e\u4f8b\u5316\u8be5\u5b57\u7b26\u4e32\u5e76\u653e\u5165\u6c60\u4e2d<\/li> \n <\/ul> <\/li> \n
\u5b9e\u73b0\u7684\u57fa\u7840<\/p> <\/li> \n
- \n
\n
- \u5b9e\u73b0\u8be5\u4f18\u5316\u7684\u57fa\u7840\u662f\u56e0\u4e3a\u5b57\u7b26\u4e32\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u53ef\u4ee5\u4e0d\u7528\u62c5\u5fc3\u6570\u636e\u51b2\u7a81\u8fdb\u884c\u5171\u4eab<\/li> \n
- \u8fd0\u884c\u65f6\u5b9e\u4f8b\u521b\u5efa\u7684\u5168\u5c40\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\u4e2d\u6709\u4e00\u4e2a\u8868\uff0c\u603b\u662f\u4e3a\u6c60\u4e2d\u6bcf\u4e2a\u552f\u4e00\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61\u7ef4\u62a4\u4e00\u4e2a\u5f15\u7528,\u8fd9\u5c31\u610f\u5473\u7740\u5b83\u4eec\u4e00\u76f4\u5f15\u7528\u7740\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\u4e2d\u7684\u5bf9\u8c61\uff0c\u6240\u4ee5\uff0c\u5728\u5e38\u91cf\u6c60\u4e2d\u7684\u8fd9\u4e9b\u5b57\u7b26\u4e32\u4e0d\u4f1a\u88ab\u5783\u573e\u6536\u96c6\u5668\u56de\u6536<\/li> \n <\/ul> <\/li> \n <\/ul> \n
2. String.intern()\u4e0e\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60<\/strong><\/h2> \n
\/**\n * Returns a canonical representation for the string object.\n * <p>\n * A pool of strings, initially empty, is maintained privately by the\n * class <code>String<\/code>.\n * <p>\n * When the intern method is invoked, if the pool already contains a\n * string equal to this <code>String<\/code> object as determined by\n * the {@link #equals(Object)} method, then the string from the pool is\n * returned. Otherwise, this <code>String<\/code> object is added to the\n * pool and a reference to this <code>String<\/code> object is returned.\n * <p>\n * It follows that for any two strings <code>s<\/code> and <code>t<\/code>,\n * <code>s.in","orderid":"0","title":"\u9762\u8bd5\u5b98\uff1aString.intern() \u6709\u4ec0\u4e48\u7528\uff1f\u548c\u5e38\u91cf\u6c60\u6709\u4ec0\u4e48\u5173\u7cfb\uff1f\u95ee\u5012\u4e00\u5927\u7247\uff01(\u4e00)","smalltitle":"","mid":"0","fname":"JAVA","special_id":"0","bak_id":"0","info":"0","hits":"57","pages":"3","comments":"0","posttime":"2023-08-26 21:10:55","list":"1693055455","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"String.intern<\/A> \u5e38\u91cf\u6c60<\/A> \u5173\u7cfb<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"220.152.140.35","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"\u9762\u8bd5\u5b98\uff1aString.intern() \u6709\u4ec0\u4e48\u7528\uff1f\u548c\u5e38\u91cf\u6c60\u6709\u4ec0\u4e48\u5173\u7cfb\uff1f\u95ee\u5012\u4e00\u5927\u7247\uff01","lastview":"1713198157","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}