包裝類
- 針對八種基本定義相應的參考型別——包裝類
- 有類的特點,就可以呼叫類中的方法,
-
包裝類的分類
基本資料型別 包裝類 boolean Boolean char Character byte Byte short Short int Integer long Long float Float double Double

-
包裝類和基本資料的轉換(裝箱和拆箱)
- jdk5 前的是手動裝箱和拆箱的方式,(裝箱:基本型別 -> 包裝類,反之拆箱)
- jdk5 后(含jdk5) 的自動裝箱和拆箱方式,
- 自動裝箱底層呼叫的是valueOf方法,比如Integer.valueOf(),
- 其他包裝類的用法類似,
public class Integer01 { public static void main(String[] args) { // 演示Integer的裝箱和拆箱 // jdk5.0 前是手動裝箱和拆箱 //手動裝箱 int -> Integer int n1 = 100; Integer integer = new Integer(n1); Integer integer1 = Integer.valueOf(n1); // 手動拆箱 Integer -> int int i = integer.intValue(); //jdk5以后,就可以自動裝箱和自動拆箱 int n2 = 200; //自動裝箱 int -> Integer Integer integer2 = n2;//在底層依舊使用的是Integer.valueOf(n2)方法,這個方法,只是優化了語法 //自動拆箱 Integer -> int int n3 = integer2;//在底層依舊使用的是intValue()方法, } } -
包裝型別和String型別的相互轉換
public class WrapperVSString { public static void main(String[] args) { //包裝類 -> String Integer i = 100;//自動裝箱 //方式(1) String str = i + ""; //方式(2) String str1 = i.toString(); //方式(3) String str3 = String.valueOf(i);//底層用的還是toString方法 //String -> 包裝類(Integer) String str4 = "1234"; //方式(1): Integer i2 = Integer.parseInt(str4);//有使用到自動裝箱,以下兩個方法實質都是利用者方式1 //方式(2): Integer i1 = Integer.valueOf(str4); //Integer.valueOf(str4) 中 valueOf方法的原始碼: //public static Integer valueOf(String s) throws NumberFormatException { // return Integer.valueOf(parseInt(s, 10)); // } //方式(3): Integer i3 = new Integer(str4); //new Integer(str4) 中 這個構造方法原始碼: //public Integer(String s) throws NumberFormatException { // this.value = https://www.cnblogs.com/zh-Note/archive/2023/01/01/parseInt(s, 10); // } } } -
Integer創建機制
Integer類內部有一個靜態內部類IntegerCache,內部有靜態成員儲存了-128到127的Integer陣列,
當利用valueOf方法創建Integer類物件時,而值又在[-128,127]范圍內時它就會回傳這個陣列中值對應的物件,
//IntegerCache原始碼 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 = https://www.cnblogs.com/zh-Note/archive/2023/01/01/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() {} }//valueOf方法 public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }預先就創建好了共256個Integer物件供后續使用,整個IntegerCache類中的代碼均被static修飾,目的是確保在被實際使用時就已經完成了初始化,IntegerCache類中使用cache[]陣列快取了從-128~127供256個Integer物件,
回到valueOf方法,可見,如果傳入的int值在cache快取-128~127的范圍中,則直接回傳預先創建好的物件,如果不在范圍中,則創建一個新的Integer物件回傳,
//案例 public void method1(){ Integer i new Integer(1); Integer j new Integer(1); System.out.printin(i==j)://False //所以,這里主要是看范圍-128~127就是直接回傳 Integer m=1;//底層Integer.valueOf(1);->閱讀原始碼 Integer n=1;//底層Integer.valueOf(1)i System.out.printin(m == n); //T //所以,這里主要是看范圍-128~127就是直接回傳 //,否則,就new Integer(xx); Integer x = 128;//底層Integer.valueOf(1): Integer y = 128;//底層Integer..valueOf(1): System.out.printin(x == y)//False }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/541031.html
標籤:其他
上一篇:包裝類
下一篇:力扣104 求二叉樹的最大深度
