JAVA中的包装类(装箱、拆箱)

JAVA中的包装类

  • java中为8种基本数据类型提供了8种包装类型,8种包装类属于引用数据类型,父类是Object
  • 8种包装类:
基本数据类型包装类型
bytejava.lang.Byte
shortjava.lang.Short
intjava.lang.Integer
longjava.lang.Long
floatjava.lang.Float
doublejava.lang.Double
booleanjava.lang.Boolean
charjava.lang.Char

8种包装类中有6个都是数字对应的包装类,他们父类都是Number,
Number是一个抽象类,无法实例化对象

  • 装箱,拆箱:
public class IntegerTest01 {
    public static void main(String[] args) {
        //基本数据类型通过构造方法达到了向引用数据类型的转换
        //基本数据类型 ——> 引用数据类型 (装箱)
        Integer integer1 = new Integer(123);  //Integer构造方法自java9弃用

        //String ——> int
//        Integer integer2 = new Integer("hello");  //"hello"无法转为int型
        Integer integer2 = new Integer("123");
        System.out.println(integer2);

        Float f1 = new Float(3.14);
        Float f2 = new Float("3");
        System.out.println(f2);

        //引用数据类型 ——>基本数据类型 (拆箱)
        float f = integer1.floatValue();
        System.out.println(f);


        //通过访问包装类的常量,来获取最大最小值
        System.out.println("int最大值:" + Integer.MAX_VALUE + " 最小值:" + Integer.MIN_VALUE);
        System.out.println("byte最大值:" + Byte.MAX_VALUE + " 最小值:" + Byte.MIN_VALUE);
        System.out.println("double最大值:" + Double.MAX_VALUE + " 最小值:" + Double.MIN_VALUE);
        System.out.println("float最大值:" + Float.MAX_VALUE + " 最小值:" + Float.MIN_VALUE);
    }
}

自动装箱,自动拆箱:

/*
自JDK1.5后,支持自动装箱了
 */
public class IntegerTest02 {
    public static void main(String[] args) {
        //自动装箱:
        //基本数据类型 ——> 包装类
        Integer i = 100;

        //自动拆箱:
        //包装类 ——> 基本数据类型
        float f = i;

        Integer i2 = 999;
        //自动拆箱:
        // +两边要求是基本数据类型数据,i2是包装类,不属于基本数据类型,
        // 这里会进行自动拆箱,将i2转换为基本数据类型
        System.out.println(i2 + 200);  //1199


        /*
        java中为了提高程序执行效率,将[-128,127]之间的所有包装对象提前创建好,
        放到了方法区中的“整数型变量池”中,目的是只要用这个区间的数据就不需要new了,
        直接从常量池中取出来。
         */

        //所以这里 x 和 y 指向的都是同一个内存地址
        Integer a = 127;
        Integer b = 127;
        System.out.println(a == b);   //true

        Integer x = 128;    //等同于Integer x = new Integer(128);
        Integer y = 128;
        System.out.println(x == y);   //false

    }
}

String类型,int类型,Integer类型 间的转换

在这里插入图片描述

/*
1.parse()是SimpleDateFomat里面的方法
parseInt()或parsefloat()顾名思义 比如说parseInt()就是把String类型转化为int类型。
如 String a= “123”;
int b = Integer.parseInt(a);
这样b就等于123了。

2.ValueOf()方法比如说 Integer.valueOf() 是把String类型转化为Integer类型(注意:是Integer类型,而不是int类型,int类型是表示数字的简单类型,Integer类型是一个引用的复杂类型)
如:
String a= “123”;
Integer c =Integer.valueOf(a);
//Integer类型可以用intValue方法转化为int类型
int b =c.intValue();
这时候这个b就等于123了

3.toString()可以把一个引用类型转化为String字符串类型。
下面举个例子与2相反,把Integer转化为String类型:
Integer a = new Integer(123);
String b =a.toString();
这时候b就是 “123” 了
 */
public class IntegerTest03 {
    public static void main(String[] args) {
        int retValue = Integer.parseInt("123");
    }
}

注释原文链接:https://blog.csdn.net/qq_40922845/article/details/100097064

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值