包裝類

定義

包裝類就是將基本數據類型封裝到類中,目的是爲了讓基本類型也能讓Object來接收。

class IntDemo {
    private int intValue;
    public IntDemo(int intValue) {
        this.intValue = intValue;
    }
    public int intValue() {
        return this.intValue;
    }
}
public class Test {       
    public static void main(String[] args) throws Exception { 
        // 將整型常量封裝到類中
        IntDemo intDemo = new IntDemo(10);
        int result = intDemo.intValue();
        System.out.println(result);
        
        Object obj = new IntDemo(5);
        IntDemo temp = (IntDemo) obj;
        System.out.println(temp.intValue());
    }
}

分類

  • 對象型包裝類(Object的直接子類):Boolean(boolean)、Character(char)

  • 數值型包裝類(Number的直接子類,存在數值轉換異常):Byte(byte)、Short(short)、Long(long)、Double(double)、Float(float)、Integer(int)

裝箱與拆箱—基本數據類型與相應包裝類的數據處理

  • 裝箱:將基本數據類型變爲包裝類對象,利用每個包裝類提供的構造方法實現裝箱

  • 拆箱:將包裝類中包裝的基本類型值取出,利用Number類提供的xxValue()實現拆箱處理

public class Test {       
    public static void main(String[] args) throws Exception { 
        // 裝箱
        Integer i = new Integer(10);
        // 拆箱
        int result = i.intValue();
        System.out.println(result+10);

        // 自動裝箱
        Integer i2 = 20;
        // 自動拆箱
        int result2 = i2+10;
        System.out.println(result2+10);
    }
}

阿里編碼規範:所有相同類型的包裝類對象之間的值比較,全部使用equals方法比較

public class Test {       
    public static void main(String[] args) throws Exception { 
        Integer i1 = new Integer(10);
        Integer i2 = 10;
        System.out.println(i1 == i2);            // false
        System.out.println(i1.equals(i2));       // true
    }
}

說明:

對於Integer value = data 在-128~127之間的賦值,Integer對象在Integer常量池產生,會複用已有對象,這個區間內的Integer值可以直接使用==判斷。除此之外的所有數據,都會在堆上產生,並不會複用已有對象。**

public class Test {       
    public static void main(String[] args) throws Exception { 
        Integer i1 = 10;
        Integer i2 = new Integer(10);
        Integer i3 = 10;
        Integer i4 = 200;
        Integer i5 = 200;

        System.out.println(i1 == i2);           
        System.out.println(i1 == i3);     
        System.out.println(i4 == i5);             
    }
}

在這裏插入圖片描述

使用int 還是Integer?

  • [強制]所有POJO類(簡單Java類:Bean,只有基本類型、構造方法、getter、setter)屬性必須使用包裝類型

  • [強制]RPC方法(遠程方法調用)的返回值和參數必須使用包裝類型

  • [推薦]所有的局部變量使用基本類型

字符串和基本類型的轉換

  • 將字符串轉爲基本類型(靜態方法)

    • 調用各個包裝類.parseXX(String str)
public class Test {       
    public static void main(String[] args) throws Exception { 
        String str = "123";
        int data = Integer.parseInt(str);
        System.out.println(str+10);
        System.out.println(data+10);   
        
        String str1 = "123.10";
        double b = Double.parseDouble(str1);
        System.out.println(b+10);   
    }
}

需要注意的是,將字符串轉爲數字的時候,字符串的組成有非數字,那麼轉換就會出現錯誤 NumberFormatException(運行時異常)

public class Test {       
    public static void main(String[] args) throws Exception { 
        String str = "123a";
        int data = Integer.parseInt(str);
        System.out.println(data+10);   
        

    }
}

在這裏插入圖片描述

public class Test {       
    public static void main(String[] args) throws Exception { 
        String str1 = "true";
        boolean b1 = Boolean.parseBoolean(str1);
        System.out.println(b1);   
        
        String str2 = "true123";
        boolean b2 = Boolean.parseBoolean(str2);
        System.out.println(b2); 
    }
}

  • 基本類型變爲字符串

    • “”+基本類型
public class Test {       
    public static void main(String[] args) throws Exception { 
        String str = ""+123;
        System.out.println(str.length());
    }
}
  • 使用String類的valueof(基本類型),此方法不產生垃圾空間(推薦,靜態方法)
public class Test {       
    public static void main(String[] args) throws Exception { 
        String str = String.valueOf(10);
        System.out.println(str);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章