日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

new Integer 和 Integer.valueOf 有什么不同

發布時間:2025/7/14 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 new Integer 和 Integer.valueOf 有什么不同 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

@Testpublic void testHashCode() throws Exception {//[1237514926=acode, 1237514926=ccode, 548246552=bcode, 835648992=dcode]//可以看到 a 和 c 是同一個對象Integer a=9;Integer b=new Integer(9);Integer c=Integer.valueOf(9);Integer d=new Integer("9");int acode=System.identityHashCode(a);int bcode=System.identityHashCode(b);int ccode=System.identityHashCode(c);int dcode=System.identityHashCode(d);List<String> codeList=new ArrayList<>();codeList.add(acode+"=acode");codeList.add(bcode+"=bcode");codeList.add(ccode+"=ccode");codeList.add(dcode+"=dcode");Collections.sort(codeList);System.out.println(codeList);//[1134517053=acode, 1368884364=ccode, 401625763=dcode, 492228202=bcode]//如果值換為了128, 結果 a 和 c 就變成了不同對象, 這是因為 Integer 的實現原理, /*** 使用 new(int x) 創建對象的時候, 如下, 會創建新對象:* public Integer(int value) {* this.value = value;* }* * Integer.valueOf(int x) 方法源碼如下, 如果 x 在 IntegerCache.low 和 IntegerCache.high * (-128~127)之間,會直接從 IntegerCache.cache[] 數組里面取, 也就是說是基本類型數值 x=9 ,而* 下面的128超出了這個范圍,會調用構造器創建新對象 :* public static Integer valueOf(int i) {* if (i >= IntegerCache.low && i <= IntegerCache.high)* return IntegerCache.cache[i + (-IntegerCache.low)];* return new Integer(i);* }*/a = 128;b = new Integer(128);c = Integer.valueOf(128);d = new Integer("128");acode = System.identityHashCode(a);bcode = System.identityHashCode(b);ccode = System.identityHashCode(c);dcode = System.identityHashCode(d);codeList = new ArrayList<>();codeList.add(acode + "=acode");codeList.add(bcode + "=bcode");codeList.add(ccode + "=ccode");codeList.add(dcode + "=dcode");Collections.sort(codeList);System.out.println(codeList);}

?

轉載于:https://my.oschina.net/wliming/blog/1512257

總結

以上是生活随笔為你收集整理的new Integer 和 Integer.valueOf 有什么不同的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。