Java 常用對象-基本類型的封裝類


2017-11-04 20:39:26

基本類型封裝類:基本類型的封裝類的好處是可以在對象中定義更多的功能方法操作該數據。

常用操作之一:用於基本數據類型與字符串的轉換。

基本類型和包裝類的對應:

byte  Byte,short  Short,int  Integer,long  Long,float  Float,double  Double,char  Character,boolean  Boolean。

下面以Integer為例:

Integer:Integer 類在對象中包裝了一個基本類型 int 的值。Integer 類型的對象包含一個 int 類型的字段。

此外,該類提供了多個方法,能在 int 類型和 String 類型之間互相轉換,還提供了處理 int 類型時非常有用的其他一些常量和方法。

*構造方法

*常用屬性

*常用方法

 

  • String和int的互轉
        //int轉String
        String s1 = String.valueOf(100);
        System.out.println(s1);

        //String轉int
        int i=Integer.parseInt(s1);
        System.out.println(i);
  •  進制轉換
        //十進制轉二進制,八進制,十六進制
        System.out.println(Integer.toBinaryString(100));
        System.out.println(Integer.toOctalString(100));
        System.out.println(Integer.toHexString(100));

        //十進制轉任何進制,進制范圍為[2,36],因為0-9,a-z只有36個。
        System.out.println(Integer.toString(100,16));
        
        //其他進制轉十進制
        //將二進制100轉十進制
        System.out.println(Integer.parseInt("100",2));
  •  JDK5新特性:自動拆裝箱
        // 自動裝箱
        Integer i=100;
        // 自動拆箱
        i+=200;
        System.out.println(i);
        
        // 通過反編譯可以得到自動拆裝箱的代碼
        // Integer i=Integer.valueOf(100);
        // i=Integer.valueOf(i.intValue()+200);
        // System.out.println(String.valueOf(i));

[注意]Integer對象不能是null。否則會出現NullPointerException。建議先判斷是否為null,再使用。

  • 數據緩沖池
        Integer i1 = new Integer(127);
        Integer i2 = new Integer(127);
        System.out.println(i1==i2);  // false
        System.out.println(i1.equals(i2));  // true

        Integer i3 = new Integer(128);
        Integer i4 = new Integer(128);
        System.out.println(i3==i4);  // false
        System.out.println(i3.equals(i4));  // true

        Integer i5 = 127;
        Integer i6 = 127;
        System.out.println(i5==i6);  //true
        System.out.println(i5.equals(i6));  // true

        Integer i7 = 128;
        Integer i8 = 128;
        System.out.println(i7==i8);  // false
        System.out.println(i7.equals(i8));  //false

在對象的比較中“==”是比較內存地址,而equals()函數默認也是比較地址,但是這里Integer類重寫了該方法,所以是比較的數值大小,因此,所有的equals是一樣的。

顯然,如果是new出來的對象其地址一定是不同的,所以前面兩個在判斷“==”的時候都是false。

那么為什么自動裝箱后會出現差別呢?

從上面的源碼分析我們得知,自動裝箱在底層其實是在調用valueof(int i)方法把int類型轉成Integer類型。valueof()的源碼如下:

static final int low = -128;
static final int high;

static {
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                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;    

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

也就是說,valueof方法在調用的時候會區別對待,在[-128,127]的數會從緩沖池中直接取,而不在這個范圍的數據則會new一個對象進行返回。

  • 類型轉換的時候,.valueOf()和.parseX()的區別

valueOf:返回的是個包裝類
parseInt:返回的就是基本數據類型

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM