首页 课程 师资 教程 报名

什么是包装类

  • 2022-08-11 10:28:21
  • 515次 星辉

Wrapper 类是其对象包装或包含原始数据类型的类。当我们为Java包装类创建一个对象时,它包含一个字段,在这个字段中,我们可以存储原始数据类型。换句话说,我们可以将原始值包装到包装类对象中。

需要包装类

它们将原始数据类型转换为对象。如果我们希望修改传递给方法的参数(因为原始类型是按值传递的),则需要对象。

java.util 包中的类只处理对象,因此包装类在这种情况下也有帮助。

Collection 框架中的数据结构,例如ArrayList和Vector,只存储对象(引用类型)而不是原始类型。

需要一个对象来支持多线程中的同步。

自动装箱和拆箱

自动装箱:将原始类型自动转换为其相应包装类的对象称为自动装箱。例如 – 将 int 转换为 Integer,将 long 转换为 Long,将 double 转换为 Double 等。

示例:

// Java program to demonstrate Autoboxing  
import java.util.ArrayList;
class Autoboxing
{
    public static void main(String[] args)
    {
        char ch = 'a';  
        // Autoboxing- primitive to Character object conversion
        Character a = ch;  
        ArrayList<Integer> arrayList = new ArrayList<Integer>();  
        // Autoboxing because ArrayList stores only objects
        arrayList.add(25);  
        // printing the values from object
        System.out.println(arrayList.get(0));
    }
}

输出:

25

拆箱:这只是自动装箱的逆过程。将包装类的对象自动转换为其相应的原始类型称为拆箱。例如 – 将 Integer 转换为 int、Long 转换为 long、Double 转换为 double 等。

// Java program to demonstrate Unboxing
import java.util.ArrayList;  
class Unboxing
{
    public static void main(String[] args)
    {
        Character ch = 'a';  
        // unboxing - Character object to primitive conversion
        char a = ch;  
        ArrayList<Integer> arrayList = new ArrayList<Integer>();
        arrayList.add(24);  
        // unboxing because get method returns an Integer object
        int num = arrayList.get(0);  
        // printing the values from primitive data types
        System.out.println(num);
    }
}

输出:

24

执行

// Java program to demonstrate Wrapping and UnWrapping
// in Java Classes
class WrappingUnwrapping
{
    public static void main(String args[])
    {
        //  byte data type
        byte a = 1;  
        // wrapping around Byte object
        Byte byteobj = new Byte(a);  
        // int data type
        int b = 10;  
        //wrapping around Integer object
        Integer intobj = new Integer(b);  
        // float data type
        float c = 18.6f;  
        // wrapping around Float object
        Float floatobj = new Float(c);  
        // double data type
        double d = 250.5;  
        // Wrapping around Double object
        Double doubleobj = new Double(d);  
        // char data type
        char e='a';  
        // wrapping around Character object
        Character charobj=e;  
        //  printing the values from objects
        System.out.println("Values of Wrapper objects (printing as objects)");
        System.out.println("Byte object byteobj:  " + byteobj);
        System.out.println("Integer object intobj:  " + intobj);
        System.out.println("Float object floatobj:  " + floatobj);
        System.out.println("Double object doubleobj:  " + doubleobj);
        System.out.println("Character object charobj:  " + charobj);  
        // objects to data types (retrieving data types from objects)
        // unwrapping objects to primitive data types
        byte bv = byteobj;
        int iv = intobj;
        float fv = floatobj;
        double dv = doubleobj;
        char cv = charobj;  
        // printing the values from data types
        System.out.println("Unwrapped values (printing as data types)");
        System.out.println("byte value, bv: " + bv);
        System.out.println("int value, iv: " + iv);
        System.out.println("float value, fv: " + fv);
        System.out.println("double value, dv: " + dv);
        System.out.println("char value, cv: " + cv);
    }
}

输出:

Wrapper 对象的值(作为对象打印)
字节对象 byteobj: 1
整数对象 intobj:10
浮动对象 floatobj:18.6
双对象doubleobj:250.5
字符对象 charobj: a
展开的值(作为数据类型打印)
字节值,bv:1
整数值,iv:10
浮点值,fv:18.6
双倍值,dv:250.5
字符值,cv:a

以上就是关于“什么是包装类”的介绍,大家如果对此比较感兴趣,想了解更多相关知识,可以关注一下星辉的Java基础教程,里面有更丰富的知识等着大家去学习,希望对大家能够有所帮助。

选你想看

你适合学Java吗?4大专业测评方法

代码逻辑 吸收能力 技术学习能力 综合素质

先测评确定适合在学习

在线申请免费测试名额
价值1998元实验班免费学
姓名
手机
提交