java自動裝箱拆箱淺析
自動裝箱拆箱概念
在Java SE 5之后,為基本數據類型提供了自動裝箱和拆箱功能,使得將基本類型轉換與對象之間的轉換變得極其便捷。 | 基本數據類型 | 包裝類 | | int(4字節) | Integer | | byte(1字節) | Byte | | short(2字節) | Short | | long(8字節) | Long | | float(4字節) | Float | | double (8字節) | Double | | char(2字節) | Character | | boolean(未定) | Boolean |Integer i = 100;(拆箱) int j = i;(裝箱)</pre>
自動裝箱拆箱的實現
Integer i = 100;(拆箱) int j = i;(裝箱) //上面兩行代碼的反編譯如下: Integer i = Integer.valueOf(100);//拆箱 int j = i.intValue();//裝箱 對于其他基本類型的裝箱和拆箱和int類似。案例分析
Integer
(1)案例 int a = 100; int b = 100; System.out.println(a == b); int c = 200; int d = 200; System.out.println(c == d); Integer e = new Integer(100); System.out.println(a == e); //結果 true true true //反編譯 byte a = 100; byte b = 100; System.out.println(a == b); short c = 200; short d = 200; System.out.println(c == d); Integer e = new Integer(100); System.out.println(a == e.intValue());如果改為下面的代碼呢? (2)案例 Integer a = 100; Integer b = 100; System.out.println(a == b); //int c = 200; Integer c = 200; Integer d = 200; System.out.println(c == d);//輸出true Integer e = new Integer(100); System.out.println(a == e); //結果 true false false //反編譯結果 Integer a = Integer.valueOf(100); Integer b = Integer.valueOf(100); System.out.println(a == b); Integer c = Integer.valueOf(200); Integer d = Integer.valueOf(200); System.out.println(c == d); Integer e = new Integer(100); System.out.println(a == e);</pre>
(1)案例和(2)案例為啥不一樣呢?
當 "=="運算符的兩個操作數都是包裝器類型的引用,則比較的是對象;而如果其中有一個操作數是表達式(即包含算術運算)或是基本類型則比較的是數值。
原因分析:我們查看一下valueOf()源碼
public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) //這里有個IntegerCache return IntegerCache.cache[i + (-IntegerCache.low)]; //當大于high小于low時才新建 return new Integer(i); } //可以得知low=-128,high=127 private static class IntegerCache { static final int low = -128; static final int high; static final Integer cache[]; static { // high value may be configured by property int h = 127; String integerCacheHighPropValue = sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); if (integerCacheHighPropValue != null) { try { int i = parseInt(integerCacheHighPropValue); i = Math.max(i, 127); // Maximum array size is Integer.MAX_VALUE h = Math.min(i, Integer.MAX_VALUE - (-low) -1); } catch( NumberFormatException nfe) { // If the property cannot be parsed into an int, ignore it. } } high = h; cache = new Integer[(high - low) + 1]; int j = low; for(int k = 0; k < cache.length; k++) cache[k] = new Integer(j++); // range [-128, 127] must be interned (JLS7 5.1.7) assert IntegerCache.high >= 127; } private IntegerCache() {} }Boolean
boolean a = true; boolean b = true; System.out.println(a == b); Boolean c = true; Boolean d = true; System.out.println(c == d); //結果 true true //反編譯 boolean a = true; boolean b = true; System.out.println(a == b); Boolean c = Boolean.valueOf(true); Boolean d = Boolean.valueOf(true); System.out.println(c == d);為什么是true?我們查看valueOf()源碼
public static Boolean valueOf(boolean b) { return (b ? TRUE : FALSE); } //注意TRUE和FALSE為常量 public static final Boolean TRUE = new Boolean(true); public static final Boolean FALSE = new Boolean(false);Double
double a = 1.0; double b = 1.0; System.out.println(a == b); Double c = 1.0; Double d = 1.0; System.out.println(c == d); //結果 true false //反編譯 double a = 1.0D; double b = 1.0D; System.out.println(a == b); Double c = Double.valueOf(1.0D); Double d = Double.valueOf(1.0D); System.out.println(c == d); //valueOf()源碼 public static Double valueOf(double d) {//創建了新對象 return new Double(d); }Long
Integer a = 1; Integer b = 2; Integer c = 3; Integer d = 3; Integer e = 321; Integer f = 321; Long g = 3L; Long h = 2L; System.out.println(c==d);//-127~128 System.out.println(e==f);//>128 System.out.println(c==(a+b));//自動拆箱 System.out.println(c.equals(a+b));//都是Integer類型 System.out.println(g==(a+b));//自動拆箱,向上轉型為long System.out.println(g.equals(a+b));//類型不匹配 System.out.println(g.equals(a+h));//向上轉型為Long //結果 true false true true true false true //反編譯 Integer a = Integer.valueOf(1); Integer b = Integer.valueOf(2); Integer c = Integer.valueOf(3); Integer d = Integer.valueOf(3); Integer e = Integer.valueOf(321); Integer f = Integer.valueOf(321); Long g = Long.valueOf(3L); Long h = Long.valueOf(2L); System.out.println(c == d); System.out.println(e == f); System.out.println(c.intValue() == a.intValue() + b.intValue()); System.out.println(c.equals(Integer.valueOf(a.intValue() + b.intValue()))); System.out.println(g.longValue() == (long)(a.intValue() + b.intValue())); System.out.println(g.equals(Integer.valueOf(a.intValue() + b.intValue()))); System.out.println(g.equals(Long.valueOf((long)a.intValue() + h.longValue()))); //Long的equals public boolean equals(Object obj) { if (obj instanceof Long) {//類型不一樣便是false return value == ((Long)obj).longValue(); } return false; }
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!