JAVA中的包装类

包装类

基本类型     包装类
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean


举例
手机出厂各种包装 为装箱
客户买到手机打开包装 为拆箱

基本数据类型-》》包装类型 此过程被称为装箱
包装类型-》》基本数据类型 此过程被称为拆箱
列举两个例子 如下:
Boolean,Integer
package packageClass;

public class BooleanTest {
    public static void main(String[] args) {
        boolean b = false;
        Boolean b1 = new Boolean(false);
        Boolean b2 = new Boolean("false");
        System.out.println(b);
        System.out.println(b1);
        System.out.println(b2);

        //boolean Boolean相互之间的转换
        //boolean-->Boolean装箱操作
        Boolean b4 = new Boolean(b);//装箱操作
        Boolean b5 = b;//自动装箱操作
        System.out.println("boolean--->>Boolean装箱操作1"+b4);
        System.out.println("boolean--->>Boolean装箱操作2"+b5);

        //Boolean-->boolean拆箱操作
        boolean b6 = b1.booleanValue();//拆箱操作
        boolean b7 = b1;//自动拆箱操作
        System.out.println("Boolean-->boolean拆箱操作1"+b4);
        System.out.println("Boolean-->boolean拆箱操作2"+b5);


    }
}
package packageClass;

public class IntegerTest {
    public static void main(String[] args) {

        int a = 1;
        Integer b = new Integer(7);
        Integer c = new Integer("7");

        //int--->>Integer 装箱
        int i1 = 2;
        Integer i2 = new Integer(i1);//装箱操作
        Integer i3 = i1;//自动装箱操作

        System.out.println("int--->>Integer装箱操作方式1-----"+i2);
        System.out.println("int--->>Integer装箱操作方式2-----"+i3);


        //Integer--->>int 拆箱
        int i4 = i2.intValue();//拆箱操作
        int i5 = i2;//自动拆箱操作
        System.out.println("Integer--->>int拆箱操作方式1-----"+i4);
        System.out.println("Integer--->>int拆箱操作方式2-----"+i5);

    }
}

 

posted @ 2025-01-27 17:55  小马哥007  阅读(13)  评论(0)    收藏  举报