天天看點

Java知識【包裝類】

目錄

​​1,包裝類​​

​​1.1:基本類型包裝類(記憶)​​

​​1.2:Integer類​​

​​1.3:自動拆箱和自動裝箱(了解)​​

​​1.4:int和String類型的互相轉換(記憶)​​

​​1.5:字元串資料排序案例(應用)​​

1,包裝類

1.1:基本類型包裝類(記憶)

  • 基本類型包裝類的作用

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

    常用的操作之一:用于基本資料類型與字元串之間的轉換

  • 基本類型對應的包裝類
基本資料類型 包裝類
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

1.2:Integer類

  • Integer類概述

    包裝一個對象中的原始類型 int 的值

  • Integer類構造方法
方法名 說明
public Integer(int value) 根據 int 值建立 Integer 對象(過時)
public Integer(String s) 根據 String 值建立 Integer 對象(過時)
public static Integer valueOf(int i) 傳回表示指定的 int 值的 Integer 執行個體
public static Integer valueOf(String s) 傳回一個儲存指定值的 Integer 對象 String

示例代碼

public class IntegerDemo {
    public static void main(String[] args) {
        //public Integer(int value):根據 int 值建立 Integer 對象(過時)
        Integer i1 = new Integer(100);
        System.out.println(i1);

        //public Integer(String s):根據 String 值建立 Integer 對象(過時)
        Integer i2 = new Integer("100");
//        Integer i2 = new Integer("abc"); //NumberFormatException
        System.out.println(i2);
        System.out.println("--------");

        //public static Integer valueOf(int i):傳回表示指定的 int 值的 Integer 執行個體
        Integer i3 = Integer.valueOf(100);
        System.out.println(i3);

        //public static Integer valueOf(String s):傳回一個儲存指定值的Integer對象 String
        Integer i4 = Integer.valueOf("100");
        System.out.println(i4);
    }
}      

1.3:自動拆箱和自動裝箱(了解)

  • 自動裝箱

    把基本資料類型轉換為對應的包裝類類型

  • 自動拆箱

    把包裝類類型轉換為對應的基本資料類型

  • 示例代碼
Integer i = 100;  // 自動裝箱
i += 200;         // i = i + 200;  i + 200 自動拆箱;i = i + 200; 是自動裝箱      

1.4:int和String類型的互相轉換(記憶)

  • 轉換方式
  • 方式一:直接在數字後加一個空字元串
  • 方式二:通過String類靜态方法valueOf()
  • 示例代碼
public class IntegerDemo {
    public static void main(String[] args) {
        //int --- String
        int number = 100;
        //方式1
        String s1 = number + "";
        System.out.println(s1);
        //方式2
        //public static String valueOf(int i)
        String s2 = String.valueOf(number);
        System.out.println(s2);
        System.out.println("--------");
    }
}      
  • 轉換方式
  • 方式一:先将字元串數字轉成Integer,再調用valueOf()方法
  • 方式二:通過Integer靜态方法parseInt()進行轉換
  • 示例代碼
public class IntegerDemo {
    public static void main(String[] args) {
        //String --- int
        String s = "100";
        //方式1:String --- Integer --- int
        Integer i = Integer.valueOf(s);
        //public int intValue()
        int x = i.intValue();
        System.out.println(x);
        //方式2
        //public static int parseInt(String s)
        int y = Integer.parseInt(s);
        System.out.println(y);
    }
}      

1.5:字元串資料排序案例(應用)

  • 案例需求

    有一個字元串:“91 27 46 38 50”,請寫程式實作最終輸出結果是:27 38 46 50 91

  • 代碼實作
public class IntegerTest {
    public static void main(String[] args) {
        //定義一個字元串
        String s = "91 27 46 38 50";

        //把字元串中的數字資料存儲到一個int類型的數組中
        String[] strArray = s.split(" ");
//        for(int i=0; i<strArray.length; i++) {
//            System.out.println(strArray[i]);
//        }

        //定義一個int數組,把 String[] 數組中的每一個元素存儲到 int 數組中
        int[] arr = new int[strArray.length];
        for(int i=0; i<arr.length; i++) {
            arr[i] = Integer.parseInt(strArray[i]);
        }

        //對 int 數組進行排序
        Arrays.sort(arr);

        for(int i=0; i<arr.length; i++){
         System.out.print(arr[i] + " ");
        }
}      

繼續閱讀