1. 程式人生 > 實用技巧 >JavaSE學習筆記13:基本型別對應的8個包裝類

JavaSE學習筆記13:基本型別對應的8個包裝類

基本型別對應的8個包裝類

1.8種包裝類存在的意義

  1. java中為8種資料型別又對應準備了8種包裝型別。8種包裝類屬於引用資料型別,父類是Object。

  2. 為什麼要提供8種包裝類呢?

    因為8種基本型別不夠用

public class IntegerTest01 {
    public static void main(String[] args) {
        /*假設有需求:呼叫doSOme()方法的時候需要傳一個數字進去,
        但是數字屬於基本資料型別,而doSOme方法引數的型別是Object
        可見doSome方法無法接收基本資料型別的數字。怎麼辦?可以傳一個數字對應的包裝類進去。
        */
        //把100這個數字經過構造方法包裝成物件
        MyInt myInt = new MyInt(100);
        //doSome方法雖然不能直接傳100,但是可以傳一個100對應的包裝型別
        doSome(myInt);
    }
    public static void doSome(Object obj){
        System.out.println(obj);
    }
    //輸出結果為:100
}
package se2.integer;

public class MyInt {
    int value;

    public MyInt() {}
    public MyInt(int value) {
        this.value = value;
    }
    //重寫toString方法
    @Override
    public String toString() {
        return String.valueOf(value);
    }
}

2.8種基本資料型別對應的包裝型別名是什麼?

基本資料型別 包裝型別
byte java.lang.Byte(父類Number)
short java.lang.Short(父類Number)
int java.lang.Integer(父類Number)
long java.lang.Long(父類Number)
float java.lang.Float(父類Number)
double java.lang.Double(父類Number)
boolean java.lang.Boolean(父類Object)
char java.lang.Character(父類Object)

以上8種包裝類中,重點以java.lang.Integer為代表進行學習,其他的型別照葫蘆畫瓢就行了

3.裝箱和拆箱的概念

Number是一個抽象類,無法例項化物件

Number類中有這樣的方法:

byte byteValue() 以byte形式返回指定的數值
abstract double doubleValue() 以double形式返回指定的數值
abstract float floatValue() 以float形式返回指定的數值
abstract int intValue() 以int形式返回指定的數值
abstract long longValue() 以long形式返回指定的數值
short shortValue() 以short形式返回指定的數值

這些方法其實所有的數字包裝類的子類都有,這些方法是負責拆箱的。

public class IntegerTest02 {
    public static void main(String[] args) {
        //123這個基本資料型別,進行構造方法的包裝達到了:基本資料型別向引用型別的轉換
        //基本資料型別(轉換)-->引用資料型別(裝箱)
        Integer i = new Integer(123);
        //將引用資料型別(轉換)-->基本資料型別(拆箱)
        float f = i.floatValue();
        System.out.println(f);//123.0
        //將引用資料型別(轉換)-->基本資料型別(拆箱)
        int retValue = i.intValue();
        System.out.println(retValue);//123
    }
}

4.自動裝箱與自動拆箱

在JDk1.5(java5)之後,支援自動拆箱和自動裝箱了

  1. 自動裝箱:基本資料型別自動轉換成包裝類
  2. 自動拆箱:包裝類自動轉換成基本資料型別
  3. 有了自動拆箱之後,Number類中的方法就用不著了!

自動裝箱和自動拆箱的好處:方便程式設計

package se2.integer;

public class IntegerTest05 {
    public static void main(String[] args) {
        //900是基本資料型別
        //x是包裝型別
        //基本資料型別--(自動轉換)-->包裝型別:自動裝箱
        Integer x = 900;
        System.out.println(x);//900

        //x是包裝型別
        //y是基本資料型別
        //包裝型別--(自動轉換)-->基本資料型別:自動拆箱
        int y = x;
        System.out.println(y);//900

        //z是一個引用,是一個變數,z還是儲存了一個物件的記憶體地址
        Integer z = 1000;//等同於:Integer z = new Integer(1000);
        //分析為什麼沒有報錯呢?
        /**
         * +號兩邊要求是基本資料型別的數字,z是包裝類,
         * 不屬於基本資料型別,這裡會自動拆箱,將z轉換
         * 成基本資料型別
         */
        System.out.println(z + 1);

        Integer a = 1000;//Integer a = new Integer(1000);a是個引用,儲存記憶體地址指向物件
        Integer b = 1000;//Integer b = new Integer(1000);b是個引用,儲存記憶體地址指向物件
        //== 比較的是物件的記憶體地址,a和b兩個引用中儲存的物件記憶體地址不同
        //== 不會觸發自動拆箱機制。(只有+-*/等運算的時候才會)
        System.out.println(a == b);//false
    }
}

擴充套件:

package se2.integer;
/*
這個題目是Integer非常重要的面試題
 */
public class IntegerTest06 {
    public static void main(String[] args) {

        Integer a = 128;
        Integer b = 128;
        System.out.println(a == b);//false
        /**
         * java中為了提高程式的執行效率,將-128到127之間所有的包裝物件提前建立好,
         * 放到了一個方法區的“整數型常量池”當中了,目的是隻要用這個區間的資料不需要
         * 再new了,直接從整數型常量池當中取出來。
         */
        //原理:x變數中儲存的物件的記憶體地址和y變數中儲存的物件的記憶體地址是一樣的
        Integer x = 127;
        Integer y = 127;
        System.out.println(x == y);//true
    }
}

5.Integer的構造方法

關於Integer類的構造方法,有兩個:

​ Integer(int)

​ Integer(String)

package se2.integer;

public class IntegerTest03 {
    public static void main(String[] args) {
        //java9之後不建議使用這個構造方法了
        //將數字100轉換成Integer包裝型別(int -->Integer)
        Integer x = new Integer(100);
        System.out.println(x);//100

        //將String型別的數字,轉換成Integer包裝型別(String -->Integer)
        Integer y = new Integer("123");
        System.out.println(y);//123

        //double --> Double
        Double d = new Double(1.23);
        System.out.println(d);//1.23
        //String --> DOuble
        Double e = new Double("3.14");
        System.out.println(e);//3.14
    }
}

通過訪問包裝類的常量,來獲取最大值和最小值,如下

package se2.integer;

public class IntegerTest04 {
    public static void main(String[] args) {
        
        System.out.println("int的最大值:" + Integer.MAX_VALUE);
        System.out.println("int的最小值:" + Integer.MIN_VALUE);
        System.out.println("byte的最大值:" + Byte.MAX_VALUE);
        System.out.println("byte的最小值:" + Byte.MIN_VALUE);
        /**輸出結果:
         * int的最大值:2147483647
         * int的最小值:-2147483648
         * byte的最大值:127
         * byte的最小值:-128
         */
    }
}

6.Integer的常用方法

package se2.integer;

public class IntegerTest07 {
    public static void main(String[] args) {
        //1手動裝箱與自動裝箱
        //手動裝箱
        Integer x = new Integer(1000);
        //手動拆箱
        Integer y = x.intValue();
        System.out.println(y);//1000

        Integer a1 = new Integer("123");
        //編譯的時候沒問題,執行時呢?
        //不是一個數字可以包裝成Interger嗎?不能!執行時會出現異常!
        //java.lang.NumberFormatException(數字格式化異常)
        //Integer a2 = new Integer("阿波");

        //2重點方法
        //static int parseInt(String s)
        //靜態方法,傳參String,返回int
        /**
         * 為什麼常用這個方法?原因如下
         * 網頁上文字框中輸入的100實際上是“100”字串,後臺資料庫中
         * 要求儲存100數字,此時java程式需要將“100”轉換成100數字。
         */
        int retValue = Integer.parseInt("123");//String -轉換->int
        System.out.println(retValue + 110);//233
        /*
        int retValue2 = Integer.parseInt("阿波");
        System.out.println(retValue2 + 100);
        //報錯:java.lang.NumberFormatException: For input string: "阿波"
        */
        //照葫蘆畫瓢
        double retValue3 = Double.parseDouble("3.14");
        System.out.println(retValue3 + 1);//4.140000000000001(精度問題)

        float retValue4 = Float.parseFloat("1.0");
        System.out.println(retValue4 + 1);//2.0

        //以下內容作為了解,不需要掌握
        /**
         * static String toBinaryString(int i)
         * 靜態的:將十進位制轉換成二進位制字串
         */
        String binaryString = Integer.toBinaryString(3);
        System.out.println(binaryString);//11(二進位制字串)

        /**
         * static String toHexString(int i)
         * 靜態的:將十進位制轉換成十六進位制的字串
         */
        //十六進位制:1 2 3 4 5 6 7 8 9 a b c d e f  10 11 12 13 14 15 16 17 18 19 1a...
        String hexString1 = Integer.toHexString(16);
        System.out.println(hexString1);//10

        String hexString2 = Integer.toHexString(17);
        System.out.println(hexString2);//11
        /**
         * static String toOctalString(int i)
         * 靜態的:將十進位制轉換成八進位制字串
         */
        String octalString = Integer.toOctalString(8);
        System.out.println(octalString);//10

        /**
         * valueOf方法作為了解
         * static Integer valueOf(int i)
         * 靜態的:int --> Integer
         */
        Integer i1 = Integer.valueOf(100);
        System.out.println(i1);//100

        /** static Integer valueOf(String s)
         * 靜態的:String --> Integer
         */
        Integer i2 = Integer.valueOf("100");
        System.out.println(i2);//100
    }
}

7.String、int、Integer之間互相轉換

package se2.integer;

public class IntegerTest08 {
    public static void main(String[] args) {
        //String --> int
        String s1 = "100";
        int i1 = Integer.parseInt(s1);
        System.out.println(i1 + 1);//101

        //int --> String
        String s2 = i1 + "";//i1還是100字串
        System.out.println(s2 + 1);//1001

        //int --> Integer
        //自動裝箱
        Integer x = 1000;

        //Integer --> int
        //自動拆箱
        int y = x;

        //String --> Integer
        Integer a = Integer.valueOf("123");

        //Integer --> String
        String b = String.valueOf(a);
    }
}