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

來源:互聯網
上載者:User

標籤:不能   null   code   char   java   ima   com   byte   -128   

2017-11-04 20:39:26

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

常用操作之一:用於基礎資料型別 (Elementary Data Type)與字串的轉換。

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

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:返回的就是基礎資料型別 (Elementary Data Type)

 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.