{"id":3301,"date":"2018-11-01T19:15:06","date_gmt":"2018-11-01T11:15:06","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3301"},"modified":"2018-11-01T19:15:06","modified_gmt":"2018-11-01T11:15:06","slug":"%e7%a5%9e%e5%a5%87%e7%9a%84-integer","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3301","title":{"rendered":"\u795e\u5947\u7684 Integer"},"content":{"rendered":"<p>\n\t\u5f53\u4f60\u8ba4\u4e3a\u4f60\u5df2\u7ecf\u65e0\u6bd4\u5f3a\u5927\u7684\u65f6\u5019\uff0c\u5f80\u5f80\u7ec6\u8282\u4f1a\u6253\u8d25\u4f60\uff01\u6700\u8fd1\u6211\u7684\u8eab\u8fb9\u5c31\u53d1\u751f\u4e86\u4e00\u4e2a\u7ec6\u8282\u51b3\u5b9a\u6210\u8d25\u7684\u4f8b\u5b50\uff01\n<\/p>\n<p>\n\t\u5148\u770b\u4e0b\u9762\u7684\u4e00\u6bb5\u4ee3\u7801\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\npublic class Xttblog {\r\n    public static void main(String[] args) {\r\n        Integer a = 1;\r\n        Integer b = 2;\r\n        Integer c = 3;\r\n        Integer d = 3;\r\n        Integer e = 321;\r\n        Integer f = 321;\r\n        Long g = 3L;\r\n        System.out.println(c == d);\r\n        System.out.println(e == f);\r\n        System.out.println(c == (a + b));\r\n        System.out.println(c.equals(a + b));\r\n        System.out.println(g == (a + b));\r\n        System.out.println(g.equals(a + b));\r\n        System.out.println(g.equals(a + h));\r\n    }\r\n}<\/pre>\n<p>\n\t\u4f60\u4eec\u53ef\u4ee5\u5148\u601d\u8003\u4e00\u4e0b\u8bd5\u7740\u5199\u51fa\u7b54\u6848\uff0c\u770b\u770b\u80fd\u4e0d\u80fd\u505a\u5bf9\u3002\n<\/p>\n<p>\n\t\u4e0b\u9762\u6211\u516c\u5e03\u6b63\u786e\u7b54\u6848\uff1a\n<\/p>\n<blockquote>\n<p>\n\t\ttrue<br \/>\n\t\tfalse<br \/>\n\t\ttrue<br \/>\n\t\ttrue<br \/>\n\t\ttrue<br \/>\n\t\tfalse<br \/>\n\t\ttrue\n\t<\/p>\n<\/blockquote>\n<p>\n\tWhat \uff1f\u4e3a\u4ec0\u4e48\u7ed3\u679c\u662f\u8fd9\u6837\uff0c\u548c\u6211\u60f3\u7684\u5b8c\u5168\u4e0d\u4e00\u6837\u554a\uff01\u8fd9\u5c31\u662f\u6211\u4eca\u5929\u8981\u8bb2\u7684\u4e3b\u9898\uff1a\u795e\u5947\u7684 Integer\uff01\n<\/p>\n<p>\n\t\u5176\u5b9e\u4e00\u70b9\u4e5f\u4e0d\u5947\u602a\uff0c\u56e0\u4e3a\u5728 Java \u4e2d\u4f1a\u6709\u4e00\u4e2a Integer \u7f13\u5b58\u6c60\uff0c\u7f13\u5b58\u7684\u5927\u5c0f\u662f\uff1a-128~127\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"\u795e\u5947\u7684 Integer\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/integer-cache.png\" style=\"width: 797px;height: 260px\" \/>\n<\/p>\n<ul>\n<li>\n\t\t\u4f7f\u7528<code>==<\/code>\u7684\u60c5\u51b5\uff1a<\/p>\n<ul>\n<li>\n\t\t\t\t\u5982\u679c\u6bd4\u8f83Integer\u53d8\u91cf\uff0c\u9ed8\u8ba4\u6bd4\u8f83\u7684\u662f<strong>\u5730\u5740\u503c<\/strong>\u3002\n\t\t\t<\/li>\n<li>\n\t\t\t\tJava\u7684Integer\u7ef4\u62a4\u4e86\u4ece<code>-128~127<\/code>\u7684\u7f13\u5b58\u6c60\n\t\t\t<\/li>\n<li>\n\t\t\t\t\u5982\u679c\u6bd4\u8f83\u7684\u67d0\u4e00\u8fb9\u6709\u64cd\u4f5c\u8868\u8fbe\u5f0f(\u4f8b\u5982a+b)\uff0c\u90a3\u4e48\u6bd4\u8f83\u7684\u662f<strong>\u5177\u4f53\u6570\u503c<\/strong>\n\t\t\t<\/li>\n<\/ul>\n<\/li>\n<li>\n\t\t\u4f7f\u7528<code>equals()<\/code>\u7684\u60c5\u51b5\uff1a<\/p>\n<ul>\n<li>\n\t\t\t\t\u65e0\u8bba\u662fInteger\u8fd8\u662fLong\u4e2d\u7684<code>equals()<\/code>\u9ed8\u8ba4\u6bd4\u8f83\u7684\u662f<strong>\u6570\u503c<\/strong>\u3002\n\t\t\t<\/li>\n<li>\n\t\t\t\tLong\u7684<code>equals()<\/code>\u65b9\u6cd5\uff0cJDK\u7684\u9ed8\u8ba4\u5b9e\u73b0\uff1a<strong>\u4f1a\u5224\u65ad\u662f\u5426\u662fLong\u7c7b\u578b<\/strong>\n\t\t\t<\/li>\n<\/ul>\n<\/li>\n<li>\n\t\t\u6ce8\u610f\u81ea\u52a8\u62c6\u7bb1\uff0c\u81ea\u52a8\u88c5\u7bb1\u95ee\u9898\u3002\n\t<\/li>\n<\/ul>\n<p>\n\t\u5728 JDK5.0 \u4ee5\u540e\uff0cJVM \u5728\u542f\u52a8\u7684\u65f6\u5019\u4f1a\u5b9e\u4f8b\u53169\u4e2a\u5bf9\u8c61\u6c60\uff0c\u8fd99\u4e2a\u5bf9\u8c61\u6c60\u5206\u522b\u7528\u6765\u5b58\u50a8\u516b\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u5305\u88c5\u7c7b(\u6bd4\u5982int\u5bf9\u5e94\u7684Integer)\u548cString\u5bf9\u8c61\uff08\u5f53\u6211\u4eec\u5728\u7a0b\u5e8f\u4e2d\u76f4\u63a5\u7528\u53cc\u5f15\u53f7\u62ec\u8d77\u6765\u4e00\u4e2a\u5b57\u7b26\u4e32\u65f6\uff0cJVM\u5c31\u5230String\u7684\u5bf9\u8c61\u6c60\u91cc\u9762\u53bb\u627e\u662f\u5426\u6709\u4e00\u4e2a\u76f8\u540c\u503c\u7684\u5bf9\u8c61\uff0c\u5982\u679c\u6709\u5c31\u62ff\u73b0\u6210\u7684\u5bf9\u8c61\uff0c\u5982\u679c\u6ca1\u6709\u5c31\u5728\u5bf9\u8c61\u6c60\u91cc\u9762\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5e76\u8fd4\u56de\uff09\u3002\n<\/p>\n<p>\n\t\u518d\u770b\u4e00\u4e0b valueOf \u65b9\u6cd5\u7684\u6e90\u7801\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\npublic static Integer valueOf(int i) {\r\n    assert IntegerCache.high &gt;= 127;\r\n    if (i &gt;= IntegerCache.low &amp;&amp; i &lt;= IntegerCache.high)\r\n        return IntegerCache.cache[i + (-IntegerCache.low)];\r\n    return new Integer(i);\r\n}<\/pre>\n<p>\n\t\u5f88\u660e\u663e\u5b58\u5728\u4e00\u4e2a\u53eb\u505aIntegerCache\u7684\u7f13\u5b58\u4e86\u76f8\u5bf9\u5e94\u7684Integer\u7684\u5b9e\u4f8b\uff0c\u5f53\u8c03\u7528 valueof \u7684\u65f6\u5019\u4f1a\u4f18\u5148\u5224\u65ad\u5bf9\u5e94\u662f\u5426\u5b58\u5728\u6307\u5b9a\u533a\u95f4\u5185 \u5982\u679c\u786e\u5b9e\u5728\u5bf9\u5e94\u533a\u95f4\u5219\u76f4\u63a5\u8fd4\u56de\u5bf9\u5e94\u7f13\u5b58\u5bf9\u8c61\u3002\n<\/p>\n<p>\n\t\u8fd9\u6837\u81ea\u7136\u53ef\u4ee5\u4f7f\u7528==\u6765\u505a\u5224\u65ad\uff01\n<\/p>\n<p>\n\t\u90a3\u4e48\u6211\u73b0\u5728\u95ee\u4f60\uff0c\u4e3a\u4ec0\u4e48\u662f-128~127\u5462\uff1f\u80fd\u4e0d\u80fd\u6539\u6210\u5176\u4ed6\u7684\u8303\u56f4\uff1f\n<\/p>\n<p>\n\t\u7b54\u6848\u5f53\u7136\u662f\u53ef\u4ee5\uff0cJVM \u7ed9\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u4e2a\u4fee\u6539\u5b83\u7684\u53c2\u6570\u3002\u53ef\u4ee5\u8bbe\u7f6e&nbsp;-ea -Djava.lang.Integer.IntegerCache.high=1000 \u53c2\u6570\u3002\n<\/p>\n<pre class=\"brush:plain;\">\r\n-ea -Djava.lang.Integer.IntegerCache.high=1000<\/pre>\n<p>\n\t<img decoding=\"async\" alt=\"-ea -Djava.lang.Integer.IntegerCache.high=1000\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/integer-cache-high.png\" style=\"width: 840px;height: 63px\" \/>\n<\/p>\n<p>\n\t\u597d\u4e86\uff0c\u4ee5\u4e0a\u8fd9\u4e9b\u4e66\u672c\u4e0a\u53ef\u80fd\u90fd\u6ca1\u6709\u3002\u90a3\u6211\u662f\u4ece\u54ea\u91cc\u5b66\u6765\u7684\u5462\uff1f\n<\/p>\n<p>\n\t\u7b54\u6848\u662f java \u7684\u5b98\u7f51\u3002\u8fd9\u4e9b\u4e1c\u897f\u8001\u5e08\u4e0d\u4f1a\u6559\u4f60\uff0c\u8001\u5e08\u53ea\u4f1a\u6559\u4f60\u5b66\u4e60\u7684\u65b9\u6cd5\uff0c\u53ea\u6709\u4f60\u81ea\u5df1\u94bb\u7814\u624d\u80fd\u5b66\u4f1a\u66f4\u591a\u7684\u672c\u9886\uff01<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=3301\">\u795e\u5947\u7684 Integer<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u5f53\u4f60\u8ba4\u4e3a\u4f60\u5df2\u7ecf\u65e0\u6bd4\u5f3a\u5927\u7684\u65f6\u5019\uff0c\u5f80\u5f80\u7ec6\u8282\u4f1a\u6253\u8d25\u4f60\uff01\u6700\u8fd1\u6211\u7684\u8eab\u8fb9\u5c31\u53d1\u751f\u4e86\u4e00\u4e2a\u7ec6\u8282\u51b3\u5b9a\u6210\u8d25\u7684\u4f8b\u5b50\uff01 \u5148\u770b\u4e0b\u9762\u7684\u4e00\u6bb5\u4ee3 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,6],"tags":[1732,1733,1700,241,327],"class_list":["post-3301","post","type-post","status-publish","format-standard","hentry","category-java","category-yyzt","tag-integer","tag-integercache","tag-java-11","tag-java-util-logging","tag-java"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3301","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3301"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3301\/revisions"}],"predecessor-version":[{"id":3302,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3301\/revisions\/3302"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}