{"id":56,"date":"2021-04-25T16:02:33","date_gmt":"2021-04-25T08:02:33","guid":{"rendered":"http:\/\/112.124.66.68\/?p=56"},"modified":"2021-04-25T16:02:33","modified_gmt":"2021-04-25T08:02:33","slug":"java%e8%87%aa%e5%8a%a8%e8%a3%85%e7%ae%b1%e4%b8%8e%e6%8b%86%e7%ae%b1","status":"publish","type":"post","link":"https:\/\/sanlangcode.com\/index.php\/2021\/04\/25\/java%e8%87%aa%e5%8a%a8%e8%a3%85%e7%ae%b1%e4%b8%8e%e6%8b%86%e7%ae%b1\/","title":{"rendered":"Java\u81ea\u52a8\u88c5\u7bb1\u4e0e\u62c6\u7bb1"},"content":{"rendered":"<p>\u88c5\u7bb1\u5c31\u662f\u81ea\u52a8\u5c06\u57fa\u672c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u5305\u88c5\u5668\u7c7b\u578b\uff08int&#8211;&gt;Integer\uff09<br \/>\n\u8c03\u7528\u65b9\u6cd5\uff1aInteger\u7684valueOf(int) \u65b9\u6cd5<br \/>\n\u62c6\u7bb1\u5c31\u662f\u81ea\u52a8\u5c06\u5305\u88c5\u5668\u7c7b\u578b\u8f6c\u6362\u4e3a\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff08Integer&#8211;&gt;int\uff09\u3002<br \/>\n\u8c03\u7528\u65b9\u6cd5\uff1aInteger\u7684intValue\u65b9\u6cd5<br \/>\n\u5728Java SE5\u4e4b\u524d\uff0c\u5982\u679c\u8981\u751f\u6210\u4e00\u4e2a\u6570\u503c\u4e3a10\u7684Integer\u5bf9\u8c61\uff0c\u5fc5\u987b\u8fd9\u6837\u8fdb\u884c\uff1a<br \/>\n<span style=\"color: #ff9900;\">Integer i = new Integer(10);<\/span><\/p>\n<p><!--more--><br \/>\n\u800c\u5728\u4eceJava SE5\u5f00\u59cb\u5c31\u63d0\u4f9b\u4e86\u81ea\u52a8\u88c5\u7bb1\u7684\u7279\u6027\uff0c\u5982\u679c\u8981\u751f\u6210\u4e00\u4e2a\u6570\u503c\u4e3a10\u7684Integer\u5bf9\u8c61\uff0c<br \/>\n\u53ea\u9700\u8981\u8fd9\u6837\u5c31\u53ef\u4ee5\u4e86\uff1a<br \/>\n<span style=\"color: #ff9900;\">Integer i = 10;<\/span><\/p>\n<p>\u9762\u8bd5\u98981\uff1a \u4ee5\u4e0b\u4ee3\u7801\u4f1a\u8f93\u51fa\u4ec0\u4e48\uff1f<\/p>\n<pre>public class Main {\npublic static void main(String[] args) {\nInteger i1 = 100;\nInteger i2 = 100;\nInteger i3 = 200;\nInteger i4 = 200;\n\nSystem.out.println(i1==i2);\nSystem.out.println(i3==i4);\n}}<\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<pre> true\n false<\/pre>\n<p>\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0\u8fd9\u6837\u7684\u7ed3\u679c\uff1f\u8f93\u51fa\u7ed3\u679c\u8868\u660ei1\u548ci2\u6307\u5411\u7684\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u800ci3\u548ci4\u6307\u5411\u7684\u662f\u4e0d\u540c\u7684\u5bf9\u8c61\u3002\u6b64<br \/>\n\u65f6\u53ea\u9700\u4e00\u770b\u6e90\u7801\u4fbf\u77e5\u7a76\u7adf\uff0c\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\u662fInteger\u7684valueOf\u65b9\u6cd5\u7684\u5177\u4f53\u5b9e\u73b0<\/p>\n<pre> public static Integer valueOf(int i) {\n if(i &gt;= -128 &amp;&amp; i &lt;= IntegerCache.high)\n return IntegerCache.cache[i + 128];\n else\n return new Integer(i);\n }<\/pre>\n<p>\u5176\u4e2dIntegerCache\u7c7b\u7684\u5b9e\u73b0\u4e3a\uff1a<\/p>\n<pre>private static class IntegerCache {\n static final int high;\n static final Integer cache[];\n static {\n final int low = -128;\n \/\/ high value may be configured by property\n int h = 127;\n if (integerCacheHighPropValue != null) {\n \/\/ Use Long.decode here to avoid invoking methods that\n \/\/ require Integer's autoboxing cache to be initialized\n int i = Long.decode(integerCacheHighPropValue).intValue();\n i = Math.max(i, 127);\n \/\/ Maximum array size is Integer.MAX_VALUE\n h = Math.min(i, Integer.MAX_VALUE - -low);\n }\n high = h;\n cache = new Integer[(high - low) + 1];\n int j = low;\n for(int k = 0; k &lt; cache.length; k++)\n cache[k] = new Integer(j++);\n }\n private IntegerCache() {}\n }<\/pre>\n<p>\u4ece\u8fd92\u6bb5\u4ee3\u7801\u53ef\u4ee5\u770b\u51fa\uff0c\u5728\u901a\u8fc7valueOf\u65b9\u6cd5\u521b\u5efaInteger\u5bf9\u8c61\u7684\u65f6\u5019\uff0c<br \/>\n\u5982\u679c\u6570\u503c\u5728[-128,127]\u4e4b\u95f4\uff0c\u4fbf\u8fd4\u6307\u5411IntegerCache.cache<br \/>\n\u4e2d\u5df2\u7ecf\u5b58\u5728\u7684\u5bf9\u8c61\u7684\u5f15\u7528\uff1b\u5426\u5219\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Integer\u5bf9\u8c61\u3002<br \/>\n\u4e0a\u9762\u7684\u4ee3\u7801\u4e2di1\u548ci2\u7684\u6570\u503c\u4e3a100\uff0c\u56e0\u6b64\u4f1a\u76f4\u63a5\u4ececache\u4e2d\u53d6\u5df2\u7ecf\u5b58\u5728\u7684\u5bf9\u8c61\uff0c<br \/>\n\u6240\u4ee5i1\u548ci2\u6307\u5411\u7684\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c<br \/>\n\u800ci3\u548ci4\u5219\u662f\u5206\u522b\u6307\u5411\u4e0d\u540c\u7684\u5bf9\u8c61<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u88c5\u7bb1\u5c31\u662f\u81ea\u52a8\u5c06\u57fa\u672c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":325,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-56","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/posts\/56","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/comments?post=56"}],"version-history":[{"count":0,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/posts\/56\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/media\/325"}],"wp:attachment":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/media?parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/categories?post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/tags?post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}