Java核心API-包装类

本文详细介绍了Java中的包装类,包括其概念、构造方法、静态常量、常用方法(如转换、toString和parseXXX),以及装箱和拆箱的概念。此外,还讨论了包装类在集合中的应用以及注意事项。
摘要由CSDN通过智能技术生成

包装类



前言

掌握包装类及装箱、拆箱概念


包装类

1、包装类的概念

1)包装类把基本类型数据转换为对象

2)每个基本类型在java.lang包中都有一个相应的包装类

import java.util.ArrayList;

public class ArrayListDemo01 {

    public static void main(String[] args) {

        ArrayList arrayList = new ArrayList();

        // 集合中不能存储基本数据类型
        // 系统会通过装箱的形式将基本数据类型直接转换为包装类对象
        arrayList.add(100);
        arrayList.add(200);
        arrayList.add(300);

    }
}

2、包装类的作用

1)提供了一系列实用的方法

2)集合不允许存放基本数据类型数据,存放数字时,要用包装类型

3、包装类的构造方法

所有包装类都可将与之对应的基本数据类型作为参数,来构造它们的实例

// public Byte(byte value)构造一个新分配的 Byte 对象,以表示指定的 byte 值
// public Short(short value)构造一个新分配的 Short 对象,用来表示指定的 short 值
// public Integer(int value)构造一个新分配的 Integer 对象,它表示指定的 int 值
// public Long(long value)构造新分配的 Long 对象,表示指定的 long 参数
// public Float(float value)构造一个新分配的 Float 对象,它表示基本的 float 参数
// public Double(double value)构造一个新分配的 Double 对象,它表示基本的 double 参数
// public Character(char value)构造一个新分配的 Character 对象,用以表示指定的 char 值
// public Boolean(boolean value)分配一个表示 value 参数的 Boolean 对象

除Character类外,其他包装类可将一个字符串作为参数构造它们的实例

public class Demo01 {

    public static void main(String[] args) {

        // 所有包装类都可将与之对应的基本数据类型作为参数,来构造它们的实例
        byte num1 = 100;
        Byte byte1 = new Byte(num1); // 创建对象
        System.out.println("num1 : " + num1); // num1 : 100
        System.out.println("byte1 : " + byte1); // byte1 : 100

        char char1 = '你';
        Character character1 = new Character(char1);
        System.out.println("char1 : " + char1); // char1 : 你
        System.out.println("character1 : " + character1); // character1 : 你

        // 除Character类外,其他包装类可将一个字符串作为参数构造它们的实例(可以查询API中构造方法)

        // 对于数值类型的包装类,这个字符串必须是数字形式,否则无法转换
        Integer integer1 = new Integer("123456"); // int类型
        // 字符串必须是数字形式,否则无法转换
        System.out.println("integer1 = " + integer1);

        // Integer interger2 = new Integer("qwerty");
        // System.out.println("interger2 = " + interger2); // 报错 NumberFormatException
        // NumberFormatException 数字格式化异常

        // 对于布尔类型的包装类,字符串内容只要是true(不区分大小写),结果就是为true
        // 其他字符串结果都是为false
        Boolean boolean1 = new Boolean("true");
        System.out.println("boolean1 : " + boolean1); // true

        Boolean boolean2 = new Boolean("TrUe");
        System.out.println("boolean2 : " + boolean2); // true

        Boolean boolean3 = new Boolean("false");
        System.out.println("boolean3 : " + boolean3); // false

        Boolean boolean4 = new Boolean("123a");
        System.out.println("boolean4 : " + boolean4); // false

        Boolean boolean5 = new Boolean("abc");
        System.out.println("boolean5 : " + boolean5); // false


        // 字符包装类构造方法中不能传递字符串
        // Character character2 = new Character("q"); // 报错

    }
}

注意事项:

1)Boolean类构造方法参数为String类型时,若该字符串内容为true(不考虑大小写),则该Boolean对象表示true,否则表示false

2)当Number(数值类型)包装类构造方法参数为String 类型时,字符串不能为null,且该字符串必须可解析为相应的基本数据类型的数据,否则编译不通过,运行时会抛出NumberFormatException异常

4、包装类中的静态常量

public class Demo02 {

    public static void main(String[] args) {

        // 包装类中的静态方法,可以通过类名直接调用
        System.out.println("byte类型数据的范围:"+Byte.MIN_VALUE+"~"+Byte.MAX_VALUE); // byte类型数据的范围:-128~127
        System.out.println("short类型数据的范围:"+Short.MIN_VALUE+"~"+Short.MAX_VALUE); // short类型数据的范围:-32768~32767
        System.out.println("int类型数据的范围:"+Integer.MIN_VALUE+"~"+Integer.MAX_VALUE); // int类型数据的范围:-2147483648~2147483647
        System.out.println("long类型数据的范围:"+Long.MIN_VALUE+"~"+Long.MAX_VALUE); // long类型数据的范围:-9223372036854775808~9223372036854775807
        System.out.println("float类型数据的范围:"+Float.MIN_VALUE+"~"+Float.MAX_VALUE); // float类型数据的范围:1.4E-45~3.4028235E38
        System.out.println("double类型数据的范围:"+Double.MIN_VALUE+"~"+Double.MAX_VALUE); // double类型数据的范围:4.9E-324~1.7976931348623157E308
        System.out.println("char类型数据的范围:"+Character.MIN_VALUE+"~"+Character.MAX_VALUE); // char类型数据的范围: ~
        //
        System.out.println("boolean类型对象:"+Boolean.TRUE); // boolean类型对象:true
        System.out.println("boolean类型对象:"+Boolean.FALSE); // boolean类型对象:false
    }
}

5、包装类中常用的方法

1)xxxValue():包装类转换成基本类型(包装类->基本类型)

2)toString():以字符串形式返回包装对象表示的基本类型数据(基本类型->字符串)

3)parsexxx():把字符串转换为相应的基本数据类型数据(Character除外)(字符串->基本类型)

4)valueOf():所有包装类都有如下方法(基本类型->包装类)

public static Type valueOf(type value)

除Character类外,其他包装类都有如下方法(字符串->包装类)

public static Type valueOf(String s)
public class Demo03 {
    /*包装类中常用的方法*/
    public static void main(String[] args) {

        // xxxValue():包装类转换成基本类型
        // Byte byte1 = new Byte("128"); // NumberFormatException 数据格式异常
        Byte byte1 = new Byte("127");
        byte num1 = byte1.byteValue();
        System.out.println("num1 = " + num1); // num1 = 127

        Short short1 = new Short("32767");
        short num2 = short1.shortValue();
        System.out.println("num2 = " + num2); // num2 = 32767

        Integer integer1 = new Integer("2147483647");
        int num3 = integer1.intValue();
        System.out.println("num3 = " + num3); // num3 = 2147483647

        Long long1 = new Long("9223372036854775807");
        // public long longValue()以 long 值的形式返回此 Long 的值
        long num4 = long1.longValue(); // 包装类中对象里的值转换成一个基本数据类型
        System.out.println("num4 = " + num4); // num4 = 9223372036854775807

        Character character1 = new Character('q');
        char num5 = character1.charValue();
        System.out.println("num5 = " + num5); // num5 = q

        Boolean boolean1 = new Boolean("1233");
        Boolean boolean2 = new Boolean("True");
        Boolean boolean3 = new Boolean("true");
        boolean result1 = boolean1.booleanValue();
        boolean result2 = boolean2.booleanValue();
        boolean result3 = boolean3.booleanValue();
        System.out.println("result1 = " + result1); // result1 = false
        System.out.println("result2 = " + result2); // result2 = true
        System.out.println("result3 = " + result3); // result3 = true


        // toString():以字符串形式返回包装对象表示的基本类型数据(基本类型->字符串)
        /*
        * public String toString()返回一个表示该 Integer 值的 String 对象
        * public static String toString(int i)返回一个表示指定整数的 String 对象
        * public static String toString(int i,int radix)返回用第二个参数指定基数表示的第一个参数的字符串表示形式*/
        String str1 = Integer.toString(200); // toString() 静态方法 用Integer类名调用
        System.out.println("str1 : " + str1); // 200是字符串

        String str2 = Character.toString('w');
        System.out.println("str2 : " + str2);

        // parsexxx():把字符串转换为相应的基本数据类型数据(Character除外)(字符串->基本类型)
        byte num6 = Byte.parseByte("123");
        System.out.println("num6 = " + num6);

        // Number类型数据的字符串转换成基本数据类型,字符串需要是数字形式化的字符串,否则不能转换,会报NumberFormatException
        // byte num7 = Byte.parseByte("abc"); // NumberFormatException 数据格式异常
        // System.out.println("num7 = " + num7);

        // Boolean类型数据的字符串转换成基本数据类型时,字符串的值是true(不区分大小写)
        // 结果就是true,其他情况都是false
        boolean bool1 = Boolean.parseBoolean("true");
        System.out.println("bool1 : " + bool1); // true
        boolean bool2 = Boolean.parseBoolean("TruE");
        System.out.println("bool1 : " + bool2); // true
        boolean bool3 = Boolean.parseBoolean("false");
        System.out.println("bool1 : " + bool3); // false
        boolean bool4 = Boolean.parseBoolean("qwer");
        System.out.println("bool1 : " + bool4); // false

        // valueOf():所有包装类都有如下方法(基本类型->包装类)
        Float afloat = Float.valueOf(12.5F);
        System.out.println("afloat : " + afloat); // afloat : 12.5
        Double aDouble = Double.valueOf(12.55);
        System.out.println("aDouble : " + aDouble); // aDouble : 12.55

        // 除Character类外,其他包装类都有如下方法(字符串->包装类)
        Boolean aBoolean = Boolean.valueOf("true");
        System.out.println("aBoolean:" + aBoolean); // aBoolean:true

        Boolean bBoolean = Boolean.valueOf("True");
        System.out.println("bBoolean:" + bBoolean); // bBoolean:true

        Boolean cBoolean = Boolean.valueOf("qwer");
        System.out.println("cBoolean:" + cBoolean); // cBoolean:false

        Boolean dBoolean = Boolean.valueOf("false");
        System.out.println("dBoolean:" + dBoolean); // dBoolean:false

        Short aShort = Short.valueOf("123");
        System.out.println("aShort = " + aShort); // aShort = 123

        Short bShort = Short.valueOf("qwe");
        System.out.println("bShort = " + bShort); // NumberFormatException 报错


    }
}

6、装箱和拆箱

基本类型和包装类的自动转换

装箱:基本类型转换为包装类的对象

拆箱:包装类对象转换为基本类型的值

7、包装类的特点

1)JDK1.5后,允许基本数据类型和包装类型进行混合数学运算

2)包装类并不是用来取代基本数据类型,在基本数据类型需要用对象表示时使用

import com.sun.deploy.net.MessageHeader;

import java.util.ArrayList;

public class Demo04 {

    public static void main(String[] args) {


        int num1 = 100;
        // 装箱:将基本类型的数据或者变量直接赋值给包装类对象
        Integer integer1 = num1;
        System.out.println("integer1 = " + integer1); // integer1 = 100

        Byte byte1 = new Byte("123");
        // 拆箱:将包装类对象直接赋值给基本数据类型的变量
        byte num2 = byte1;
        System.out.println("num2 = " + num2); // num2 = 123

        /*
        * 有了装箱和拆箱的操作,可以对包装类对象和基本数据类型进行算术运算
        * */
        int num3 = 300;
        Integer integer2 = 500;
        int sum1 = num3 + integer2;
        System.out.println("sum1 = " + sum1);

        Integer sum2 = num3 + integer2;
        System.out.println("sum2 = " + sum2);

        /*
        * 集合中不能存储基本数据类型,系统会通过装箱的形式将基本数据类型直接转换为包装类对象*/
        ArrayList arrayList = new ArrayList();
        arrayList.add(100);
        arrayList.add(200);
        arrayList.add(300);

    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值