【自动装箱、自动拆箱 装装装 拆拆拆】

博客围绕自动装箱和拆箱展开,但具体内容缺失。自动装箱和拆箱是编程中的重要概念,在多种编程语言里有应用,能简化数据类型转换操作,提高编程效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class TestAutoBox {

    public static void main(String[] args) {
        Integer a=234;  //自动装箱。Integer a=Integer.valueOf(234);
        int b=a;    //自动拆箱。编译器会修改成:int b=a.intValue();

        Integer c=null;
        if(c!=null) {
            int d = c;    //自动拆箱。调用了c.intValue
        }

        //缓存[-128,127].实际就是系统初始的时候,创建了[-128,127]之间的一个缓存数组
        //当我们调用valueOf()时候,首先检查是否在[-128,127]之间,如果在这个范围则直接从缓存数组中拿出已经建好的对象
        //如果不在这个范围,则创建新的Integer对象
        Integer in1=-128;
        Integer in2=-128;
        System.out.println(in1==in2);   //true是因为123在缓存范围内
        System.out.println(in1.equals(in2));    //true
        
        System.out.println("###############");
        
        Integer in3=1234;
        Integer in4=1234;
        System.out.println(in3==in4);   //false因为1234不在缓存范围内
        System.out.println(in3.equals(in4));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值