成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

漫畫:Integer 竟然有 4 種比較方法?

開發(fā) 前端
在 Integer 的取值在 -128 到 127 之間時,它會復(fù)用已有的對象,因此在 i1(127)和 i2 使用 == 對比時值才會為 true,而當(dāng)取值變?yōu)?128 時,則執(zhí)行的結(jié)果為 false。

 本文轉(zhuǎn)載自微信公眾號「Java中文社群」,作者磊哥 。轉(zhuǎn)載本文請聯(lián)系 Java中文社群公眾號。

 

 

 

 

代碼測試

  1. public class IntegerTest { 
  2.     public static void main(String[] args) { 
  3.         Integer i1 = 127; 
  4.         Integer i2 = 127; 
  5.         System.out.println(i1 == i2); 
  6.         Integer i3 = 128; 
  7.         Integer i4 = 128; 
  8.         System.out.println(i3 == i4); 
  9.     } 

以上代碼的執(zhí)行結(jié)果為:

  1. true 
  2. false 

 

首先,當(dāng)我們將以上的測試代碼編譯為字節(jié)碼(.class)之后,編碼的代碼如下:

  1. public class IntegerTest { 
  2.   public static void main(String[] paramArrayOfString) { 
  3.     Integer integer1 = Integer.valueOf(127); 
  4.     Integer integer2 = Integer.valueOf(127); 
  5.     System.out.println((integer1 == integer2)); 
  6.     Integer integer3 = Integer.valueOf(128); 
  7.     Integer integer4 = Integer.valueOf(128); 
  8.     System.out.println((integer3 == integer4)); 
  9.   } 

可以看出在創(chuàng)建 Integer 時使用到了 valueOf,它的實現(xiàn)源碼如下:

  1. public static Integer valueOf(int i) { 
  2.     if (i >= IntegerCache.low && i <= IntegerCache.high) 
  3.         return IntegerCache.cache[i + (-IntegerCache.low)]; 
  4.     return new Integer(i); 

從上述源碼中可以看出這個方法中使用了 IntegerCache,IntegerCache 的源碼如下:

  1. private static class IntegerCache { 
  2.     static final int low = -128; 
  3.     static final int high; 
  4.     static final Integer cache[]; 
  5.  
  6.     static { 
  7.         // high value may be configured by property 
  8.         int h = 127; 
  9.         String integerCacheHighPropValue = 
  10.             sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); 
  11.         if (integerCacheHighPropValue != null) { 
  12.             try { 
  13.                 int i = parseInt(integerCacheHighPropValue); 
  14.                 i = Math.max(i, 127); 
  15.                 // Maximum array size is Integer.MAX_VALUE 
  16.                 h = Math.min(i, Integer.MAX_VALUE - (-low) -1); 
  17.             } catch( NumberFormatException nfe) { 
  18.                 // If the property cannot be parsed into an intignore it. 
  19.             } 
  20.         } 
  21.         high = h; 
  22.  
  23.         cache = new Integer[(high - low) + 1]; 
  24.         int j = low; 
  25.         for(int k = 0; k < cache.length; k++) 
  26.             cache[k] = new Integer(j++); 
  27.  
  28.         // range [-128, 127] must be interned (JLS7 5.1.7) 
  29.         assert IntegerCache.high >= 127; 
  30.     } 
  31.  
  32.     private IntegerCache() {} 

從上述源碼可以看出,在 Integer 的取值在 -128 到 127 之間時,它會復(fù)用已有的對象,因此在 i1(127)和 i2 使用 == 對比時值才會為 true,而當(dāng)取值變?yōu)?128 時,則執(zhí)行的結(jié)果為 false。

這一點其實在阿里巴巴的《Java開發(fā)手冊》中也有相應(yīng)的規(guī)定,規(guī)定的內(nèi)容如下:

【強制】所有整型包裝類對象之間值的比較,全部使用 equals 方法比較。

說明:對于 Integer var = ? 在 -128 至 127 之間的賦值,Integer 對象是在 IntegerCache.cache 產(chǎn)生, 會復(fù)用已有對象,這個區(qū)間內(nèi)的 Integer 值可以直接使用 == 進行判斷,但是這個區(qū)間之外的所有數(shù)據(jù),都 會在堆上產(chǎn)生,并不會復(fù)用已有對象,這是一個大坑,推薦使用 equals 方法進行判斷。

注意事項

不僅如此,當(dāng)我們使用 new Integer 時,無論值為多少都不能使用 == 比較,示例代碼如下:

  1. public class IntegerTest { 
  2.     public static void main(String[] args) { 
  3.         Integer i1 = new Integer(127); 
  4.         Integer i2 = new Integer(127); 
  5.         System.out.println(i1 == i2); 
  6.     } 

以上代碼的執(zhí)行結(jié)果為:

  • false

這是因為 new Integer 方法并沒有使用到 IntegerCache,而是直接創(chuàng)建了新對象,因此就不能用 == 比較了。

小貼士:== 是用來直接比對兩個對象的引用是否相同的,而 equals 則是用來對比兩個對象的值是否相同的。

 

其他比較方式

compareTo

因為 Integer 類實現(xiàn)了 Comparable 接口,因此我們可以使用 compareTo 來對比兩個值的大小,實現(xiàn)源碼如下:

  1. public final class Integer extends Number implements Comparable<Integer> { 
  2.  // 忽略其他內(nèi)容 

compareTo 的使用如下:

  1. public class IntegerTest { 
  2.     public static void main(String[] args) { 
  3.         Integer i1 = new Integer(128); 
  4.         Integer i2 = new Integer(128); 
  5.         System.out.println(i1.compareTo(i2)); 
  6.     } 

以上代碼的執(zhí)行結(jié)果為:

  • 0

compareTo 的源碼如下:

  1. public int compareTo(Integer anotherInteger) { 
  2.     return compare(this.value, anotherInteger.value); 
  3. public static int compare(int x, int y) { 
  4.     return (x < y) ? -1 : ((x == y) ? 0 : 1); 

由此可以看出 compareTo 的返回值總共有三個:-1、0、1,其中 -1 表示前一值小于后一個值;0 表示兩個值相等;1 表示前一個值大于后一個值,因此我們用它來比較兩個 Integer 的值是否相等。

直接運算

compareTo 方法給我們了一個啟發(fā),我們可以直接將兩個值進行相減,如果相減的值等于 0,則說明對比的兩個值是相同的,示例代碼如下:

  1. public class IntegerTest { 
  2.     public static void main(String[] args) { 
  3.         Integer i1 = new Integer(128); 
  4.         Integer i2 = new Integer(128); 
  5.         System.out.println((i1 - i2) == 0); 
  6.     } 

以上代碼的執(zhí)行結(jié)果為:

  • true

擴展知識:IntegerCache 值域修改

IntegerCache 默認的取值范圍為 -128 到 127,但我們可以通過設(shè)置啟動參數(shù)來調(diào)整 IntegerCache 的最大緩存值,比如我們可以配置虛擬機的啟動參數(shù) -XX:AutoBoxCacheMax=1000,此配置表示將緩存的最大值設(shè)置為 1000,如果是 Idea 的配置如下:

 

 

此時我們編寫一個測試代碼:

  1. public class IntegerTest { 
  2.     public static void main(String[] args) { 
  3.         Integer i1 = 999; 
  4.         Integer i2 = 999; 
  5.         System.out.println(i1 == i2); 
  6.     } 

以上代碼的執(zhí)行結(jié)果為:

  • true

從運行的結(jié)果可以看出 IntegerCache 的取值范圍被成功的更改了。

總結(jié)

本文我們介紹了 Integer 的四種比較方式:==、equals、compareTo、直接運算,而 == 方式并不能用于 Integer 的比較,它只適用于非 new Integer 的一定范圍內(nèi)(-128~127),而后三種方式都可以正常用于 Integer 的比較,其中 equals 的比較方式是最簡單也是最通用的。

 


原文鏈接:https://mp.weixin.qq.com/s/b9D_iaxX6SrNrw3WO55nVw

 

責(zé)任編輯:武曉燕 來源: Java中文社群
相關(guān)推薦

2022-02-14 12:04:43

前綴SpringJpa

2021-07-05 18:05:40

SpringBean方法

2020-11-03 06:57:10

MyBatis數(shù)據(jù)庫

2019-09-18 15:20:16

MyBatisSQL數(shù)據(jù)庫

2020-11-27 09:16:21

BlockingQue

2022-09-04 12:43:03

算法裁員Meta

2019-06-14 08:48:46

Tomcat日志SpringBoot

2015-07-20 15:26:56

WiFi感知

2021-12-08 08:30:55

Java AQS機制 Java 基礎(chǔ)

2009-04-13 09:09:53

WebServices返回數(shù)據(jù)橫向

2021-05-07 05:34:25

Windows10操作系統(tǒng)微軟

2021-02-03 20:19:08

Istio流量網(wǎng)格

2016-06-07 09:23:05

瀏覽器技巧快捷鍵

2017-03-07 17:45:42

Windows磁盤碎片整理

2018-06-15 14:28:36

華為云

2020-11-02 08:35:59

內(nèi)存數(shù)據(jù)庫Redis

2021-06-10 09:00:33

單例模式數(shù)據(jù)庫

2018-09-11 08:05:44

千兆路由器廠商

2011-08-01 14:54:24

編程語言

2013-03-19 09:48:55

點贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: av一二三四 | 久久av综合| 亚洲一区久久 | 久久精品久久久 | 国产高清一区二区 | 99久久婷婷国产综合精品首页 | 国产精品久久国产精品 | 久久久久亚洲 | 免费能直接在线观看黄的视频 | 国产一区二区精品在线 | 午夜国产 | 欧美成人久久 | 国产亚洲精品综合一区 | 久久国产一区二区三区 | 黄色毛片免费视频 | 黄色一级免费 | 高清色 | 久久精品国产免费 | 欧美精品99 | 精品国产一区二区在线 | 国际精品鲁一鲁一区二区小说 | 色免费视频 | 成年人在线观看视频 | 五月婷婷在线视频 | 视频精品一区二区三区 | caoporn地址| 中文字幕爱爱视频 | 黄久久久 | 久久一区二区三区免费 | 一级国产精品一级国产精品片 | 久久伊人精品一区二区三区 | 男人的天堂avav | 久久精品| 国产三级国产精品 | 亚洲精品一区中文字幕 | 亚洲国产成人在线 | 亚洲欧美一区二区三区国产精品 | 九九九国产 | 欧美一级二级视频 | 成年人在线观看 | 黄片毛片免费看 |