Java 基礎(chǔ) 31 包裝類

1.1 基本類型包裝類的概述

??需求:我要判斷一個數(shù)據(jù)是否在int范圍內(nèi)?
要想判斷一個數(shù)據(jù)是否在int范圍內(nèi),首先我們得知道int范圍,在前面我們講解基本數(shù)據(jù)類型的時候說過了:

  • -2147483648 到 2147483647

為了對基本數(shù)據(jù)類型進(jìn)行更多更方便的操作,Java就針對每一種基本數(shù)據(jù)類型提供了一個對應(yīng)的引用類型。

基本類型包裝類:

引用類型 基本數(shù)據(jù)類型
Byte byte
Short short
Integer int
Long long
Float float
Double double
Character char
Boolean boolea

??基本數(shù)據(jù)類型包裝類最常見的用法就是用于和字符串之間進(jìn)行相互轉(zhuǎn)換。

1.2 Integer類的概述和構(gòu)造方法

Integer:

  • Integer類在對象中包裝了一個基本類型 int 的值。

構(gòu)造方法:

  • Integer(int value)
  • Integer(String s)

注意:這個字符串必須由數(shù)字字符組成

1.2.1 案例代碼

package com.itheima_02;
/*
 * Integer:Integer類在對象中包裝了一個基本類型 int 的值。
 * 
 * 構(gòu)造方法:
 *      Integer(int value) 
 *      Integer(String s) 
 *          注意:這個字符串必須由數(shù)字字符組成
 */
public class IntegerDemo {
    public static void main(String[] args) {
        //Integer(int value) 
        int value = 100;
        Integer i = new Integer(value);
        System.out.println(i); //100
        System.out.println("------------");
        
        //Integer(String s) 
        String s = "100";
        //NumberFormatException:數(shù)據(jù)格式化異常
        //String s = "abc";
        Integer ii = new Integer(s);
        System.out.println(ii);
    }
}

1.3 int類型和String類型的相互轉(zhuǎn)換

??int類型和String類型的相互轉(zhuǎn)換

  • int -- String

    • String類中:public static String valueOf(int i)
  • String -- int

    • Integer類中:public static int parseInt(String s)

1.3.1 案例代碼

package com.itheima_03;
/*
 * int類型和String類型的相互轉(zhuǎn)換
 * 
 * int  --  String
 *      String類中:public static String valueOf(int i)
 * 
 * String   --  int
 *      Integer類中:public static int parseInt(String s)
 */
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("--------------");
        
        //String  -- int
        String s = "100";
        //方式1
        //String -- Integer -- int
        Integer i = new Integer(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.4 Integer 的練習(xí)之把字符串中的數(shù)據(jù)排序

需求:

??我有如下一個字符串:”91 27 46 38 50”

??請寫代碼實現(xiàn)最終輸出結(jié)果是:”27 38 46 50 91”

??提示:這里需要參考String類中的方法

??public String[] split(String regex)

1.4.1 案例代碼

package com.itheima_04;

import java.util.Arrays;

/*
 * 我有如下一個字符串:”91 27 46 38 50”
 * 請寫代碼實現(xiàn)最終輸出結(jié)果是:”27 38 46 50 91”
 * 提示:這里需要參考String類中的方法
 * public String[] split(String regex)
 * 
 * 分析:
 *      A:定義一個字符串對象
 *      B:把字符串中的數(shù)字?jǐn)?shù)據(jù)存儲到一個int類型的數(shù)組中
 *      C:對int數(shù)組進(jìn)行排序
 *      D:把排序后的數(shù)組中的元素進(jìn)行拼接得到一個字符串
 *      E:輸出字符串
 */
public class IntegerDemo {
    public static void main(String[] args) {
        //定義一個字符串對象
        String s = "91 27 46 38 50";
        
        //把字符串中的數(shù)字?jǐn)?shù)據(jù)存儲到一個int類型的數(shù)組中
        //public String[] split(String regex)
        String[] strArray = s.split(" ");
        /*
        for(int x=0; x<strArray.length; x++) {
            System.out.println(strArray[x]);
        }
        */
        
        //定義一個int類型的數(shù)組
        int[] arr = new int[strArray.length];
        for(int x=0; x<arr.length; x++) {
            arr[x] = Integer.parseInt(strArray[x]);
        }
        
        //對int數(shù)組進(jìn)行排序
        Arrays.sort(arr);
        
        //把排序后的數(shù)組中的元素進(jìn)行拼接得到一個字符串
        StringBuilder sb = new StringBuilder();
        for(int x=0; x<arr.length; x++) {
            if(x==arr.length-1) {
                sb.append(arr[x]);
            }else {
                sb.append(arr[x]).append(" ");
            }
        }
        String result = sb.toString();
        
        //輸出字符串
        System.out.println("result:"+result);
    }

1.5 JDK5的新特性自動裝箱和拆箱

JDK5新特性:

  • 自動裝箱:

    • 把基本數(shù)據(jù)類型轉(zhuǎn)換為對應(yīng)的包裝類類型
      public static Integer valueOf(int i)
  • 自動拆箱

    • 把包裝類類型轉(zhuǎn)換為對應(yīng)的基本數(shù)據(jù)類型
      public int intValue()
  • Java程序的運(yùn)行:
    編寫java文件 -- 編譯生成class文件 -- 執(zhí)行

  • 注意:

    • 在使用包裝類類型的新特性的時候,如果做操作,最好先判斷是否為null。
      開發(fā)中的原則:
  • 注意:只要是對象,在使用前就必須進(jìn)行不為null的判斷。

1.5.1 案例代碼

package com.itheima_05;
/*
 * JDK5新特性:
 * 自動裝箱:把基本數(shù)據(jù)類型轉(zhuǎn)換為對應(yīng)的包裝類類型
 *      public static Integer valueOf(int i)
 * 自動拆箱:把包裝類類型轉(zhuǎn)換為對應(yīng)的基本數(shù)據(jù)類型
 *      public int intValue()
 * 
 * Java程序的運(yùn)行:
 *      編寫java文件 -- 編譯生成class文件 -- 執(zhí)行
 * 
 * 注意:在使用包裝類類型的新特性的時候,如果做操作,最好先判斷是否為null。
 * 
 * 開發(fā)中的原則:
 *      只要是對象,在使用前就必須進(jìn)行不為null的判斷。
 */
public class IntegerDemo {
    public static void main(String[] args) {
        //創(chuàng)建一個包裝類類型的對象
        //Integer i = new Integer(100);
        Integer ii = 100; //自動裝箱    
        //public static Integer valueOf(int i)
        ii += 200; //ii = ii + 200; 自動拆箱,自動裝箱
        //public int intValue()
        System.out.println(ii);
        
        /*
        Integer ii = Integer.valueOf(100);
        ii = Integer.valueOf(ii.intValue() + 200);
        System.out.println(ii);
        */
        
        Integer iii = null;
        if(iii != null) {
            iii += 300; //NullPointerException
            System.out.println(iii);
        }
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容