{"id":10747,"date":"2023-11-28T21:42:51","date_gmt":"2023-11-28T13:42:51","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=10747"},"modified":"2023-11-28T21:42:55","modified_gmt":"2023-11-28T13:42:55","slug":"java-zhongint-heinteger-you-shen-me-qu-bie","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/java-zhongint-heinteger-you-shen-me-qu-bie\/","title":{"rendered":"Java\u4e2dint\u548cInteger\u6709\u4ec0\u4e48\u533a\u522b?"},"content":{"rendered":"<p>Java\u662f\u4e00\u4e2a\u8fd1\u4e4e\u7eaf\u6d01\u7684\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u8bed\u8a00\uff0c\u4f46\u662f\u4e3a\u4e86\u7f16\u7a0b\u7684\u65b9\u4fbf\u8fd8\u662f\u5f15\u5165\u4e86\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u4e3a\u4e86\u80fd\u591f\u5c06\u8fd9\u4e9b\u57fa\u672c\u6570\u636e\u7c7b\u578b\u5f53\u6210\u5bf9\u8c61\u64cd\u4f5c\uff0cJava\u4e3a\u6bcf\u4e00\u4e2a\u57fa\u672c\u6570\u636e\u7c7b\u578b\u90fd\u5f15\u5165\u4e86\u5bf9\u5e94\u7684\u5305\u88c5\u7c7b\u578b(wrapper class) , int\u7684\u5305\u88c5\u7c7b\u5c31\u662fInteger,Java 5\u5f00\u59cb\u5f15\u5165\u4e86\u81ea\u52a8\u88c5\u7bb1\/\u62c6\u7bb1\u673a\u5236\uff0c\u4f7f\u5f97\u4e8c\u8005\u53ef\u4ee5\u76f8\u4e92\u8f6c\u6362\u3002<br \/>\nJava\u4e3a\u6bcf\u4e2a\u539f\u59cb\u7c7b\u578b\u63d0\u4f9b\u4e86\u5305\u88c5\u7c7b\u578b\uff1a<br \/>\n1\u3001\u539f\u59cb\u7c7b\u578b:boolean,char,byte,short,int,long,float,double<br \/>\n2\u3001\u5305\u88c5\u7c7b\u578b:Boolean,Character,Byte,Short,Integer,Long,Float,Double<br \/>\nclass AutoUnboxingTest{<br \/>\npublic static void main (String[] args){<br \/>\nInteger a = new Integer(3);<br \/>\nInteger b = 3;\/\/\u5c063\u81ea\u52a8\u88c5\u7bb1\u6210Integer \u7c7b\u578b<br \/>\nint c= 3;<br \/>\nSystem.out.println(a == b); \/\/ false\u4e24\u4e2a\u5f15\u7528\u6ca1\u6709\u5f15\u7528\u540c\u4e00\u5bf9\u8c61<br \/>\nSystem.out.println(a == c); \/\/ true a\u81ea\u52a8\u62c6\u7bb1\u6210int\u7c7b\u578b\u518d\u548cc\u6bd4\u8f83<br \/>\n}<br \/>\n}<br \/>\n\u6700\u8fd1\u8fd8\u9047\u5230\u4e00\u4e2a\u9762\u8bd5\u9898\uff0c\u4e5f\u662f\u548c\u81ea\u52a8\u88c5\u7bb1\u548c\u62c6\u7bb1\u6709\u70b9\u5173\u7cfb\u7684\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a:<br \/>\npublic class Test03{<br \/>\npublic static void main(String[] args){<br \/>\nInteger f1 = 100,f2 = 100,f3 = 150,f4 = 150;<br \/>\nSystem.out.println(f1 == f2);<br \/>\nSystem.out.println(f3 == f4);<br \/>\n}<br \/>\n}<br \/>\n\u5982\u679c\u4e0d\u660e\u5c31\u91cc\u5f88\u5bb9\u6613\u8ba4\u4e3a\u4e24\u4e2a\u8f93\u51fa\u8981\u4e48\u90fd\u662ftrue\u8981\u4e48\u90fd\u662ffalse\u3002\u9996\u5148\u9700\u8981\u6ce8\u610f\u7684\u662ff1\u3001f2\u3001f3\u3001f4\u56db\u4e2a\u53d8\u91cf\u90fd\u662fInteger\u5bf9\u8c61\u5f15\u7528\uff0c\u6240\u4ee5\u4e0b\u9762\u7684==\u8fd0\u7b97\u6bd4\u8f83\u7684\u4e0d\u662f\u503c\u800c\u662f\u5f15\u7528\u3002\u88c5\u7bb1\u7684\u672c\u8d28\u662f\u4ec0\u4e48\u5462\uff1f<br \/>\n\u5f53\u6211\u4eec\u7ed9\u4e00\u4e2aInteger\u5bf9\u8c61\u8d4b\u503c\u4e00\u4e2aint\u503c\u7684\u65f6\u5019\uff0c\u4f1a\u8c03\u7528Integer\u7c7b\u7684\u9759\u6001\u65b9\u6cd5valueOf()\uff0c\u5982\u679c\u770b\u770bvalueOf()\u7684\u6e90\u4ee3\u7801\u5c31\u77e5\u9053\u53d1\u751f\u4e86\u4ec0\u4e48\u3002<br \/>\npublic static Integer valueOf(int i){<br \/>\nif(i &gt;= IntegerCache.low &amp;&amp; i &lt;= IntegerCache.high)<br \/>\nreturn IntegerCache.cache[i +(-IntegerCache.low)];<br \/>\nreturn new Integer(i);<br \/>\n}<br \/>\nIntegerCache\u662fInteger\u7684\u5185\u90e8\u7c7b\uff0c\u5176\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<br \/>\n\/*<\/p>\n<ul>\n<li>Cache to support the object identity semantics of autoboxing for values between<\/li>\n<li>-128 and 127 (inclusive) as required by JLS.<\/li>\n<li><\/li>\n<li>The cache is initialized on first usage\u3001The size of the cache<\/li>\n<li>may be controlled by the {@code -XX:AutoBoxCacheMax= <size>} option.<\/li>\n<li>During VM initialization, java.lang.Integer.IntegerCache.high property<\/li>\n<li>may be set and saved in the private system properties in the<\/li>\n<li>sun.misc.VM class.<br \/>\n*\/<br \/>\nprivate static class IntegerCache {<br \/>\nstatic final int low = -128;<br \/>\nstatic final int high;<br \/>\nstatic final Integer cache[];<br \/>\nstatic {<br \/>\n\/\/ high value may be configured by property<br \/>\nint h = 127;<br \/>\nstring integerCacheHighPropValue =<br \/>\nsun.misc.VM.getSavedProperty(&quot;java.lang.Integer.IntegerCache.high&quot;);<br \/>\nif (integerCacheHighPropValue != null){<br \/>\ntry {<br \/>\nint i = parseInt(integerCacheHighPropValue);<br \/>\ni = Math.max(i,127);<br \/>\n\/\/ Maximum array size is Integer.MAX_VALUE<br \/>\nh = Math.min(i,Integer.MAX_VALUE-(-low)-1);<br \/>\n} catch( NumberFormatException nfe){<br \/>\n\/\/ If the property cannot be parsed into an int,ignore it.<br \/>\n}<br \/>\n}<br \/>\nhigh = h;<br \/>\ncache = new Integer[(high &#8211; low)+1];<br \/>\nint j = low;<br \/>\nfor(int k=0;k&lt;cache.length;k++)<br \/>\ncache[k] = new Integer(j++);<br \/>\n\/\/range [-128,127] must be interned (JLS7 5.1.7)<br \/>\nassert IntegerCache.high &gt;=127;<br \/>\n}<br \/>\nprivate IntegerCache() {}<br \/>\n}<br \/>\n\u7b80\u5355\u7684\u8bf4,\u5982\u679c\u6574\u578b\u5b57\u9762\u91cf\u7684\u503c\u5728-128 \u5230127\u4e4b\u95f4,\u90a3\u4e48\u4e0d\u4f1anew \u65b0\u7684 Integer\u5bf9\u8c61\uff0c\u800c\u662f\u76f4\u63a5\u5f15\u7528\u5e38\u91cf\u6c60\u4e2d\u7684Integer\u5bf9\u8c61\uff0c\u6240\u4ee5\u4e0a\u9762\u7684\u9762\u8bd5\u9898\u4e2df1f2\u7684\u7ed3\u679c\u662f true\uff0c\u800cf3f4\u7684\u7ed3\u679c\u662ffalse\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Java\u662f\u4e00\u4e2a\u8fd1\u4e4e\u7eaf\u6d01\u7684\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u8bed\u8a00\uff0c\u4f46\u662f\u4e3a\u4e86\u7f16\u7a0b\u7684\u65b9\u4fbf\u8fd8\u662f\u5f15\u5165\u4e86\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u4e3a\u4e86\u80fd\u591f\u5c06\u8fd9\u4e9b\u57fa\u672c\u6570\u636e\u7c7b\u578b [&hellip;]<\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[99],"tags":[],"class_list":["post-10747","post","type-post","status-publish","format-standard","hentry","category-javabase"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/10747","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=10747"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/10747\/revisions"}],"predecessor-version":[{"id":10748,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/10747\/revisions\/10748"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=10747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=10747"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=10747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}