包装类,自动装箱与拆箱,缓存

1.包装类 

public class Wang {
    public static void main(String[] args) {
        //基本数据类型转成包装类对象;
        Integer a=new Integer(3);
        Integer b=Integer.valueOf(30);

        //把包装类对象转成基本数据类型;
        int c=b.intValue();
        double d=b.doubleValue();

        //把字符串转为包装类对象;
        Integer f=new Integer("9999");
        Integer g=Integer.parseInt("998");

        //把包装类对象转成字符串;
        String str=f.toString();//""+f

        //常见的常量;
        System.out.println("int类型最大的整数:"+Integer.MAX_VALUE);
    }
}


2. 自动装箱与拆箱,缓存

public class Wang {
    public static void main(String[] args) {
        Integer a=18;//自动装箱:Integer a=Integer.valueOf(18);
        int b=a;//自动拆箱:int b=a.intValue();


/*
        Integer 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 因为-128在缓存范围内;
        System.out.println(in1.equals(in2));//true

        Integer in3=1234;
        Integer in4=1234;
        System.out.println(in3==in4);//false 因为1234不在缓存范围内
        System.out.println(in3.equals(in4));//true
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值