Integer类型浅析案例

public class IntegerDemo {
    /***
     * 1.基本类型 只要数值相等,== 就为 true;
     * 2.包装类型 new 的对象,是新建实例放在 堆中.
     * 3.包装类型 字面量 获取 在-128 ~ 127 之间,不会新建对象,使用常量池中的对象.超过就会new 新实例.
     * 4.int和Integer之间存在自动装箱和拆箱的过程.在-128 ~ 127 之间,不会新建对象,使用常量池中的对象.超过就会new 新实例.
     *
     *
     * */
    public static void main(String[] args) {
        //基本数据类型
        int a =100;
        int b =100;

        int c =200;
        int c2 =200;

        System.out.println("基本数据类型");
        System.out.println(a==b); //true
        System.out.println(c==c2); //true

        //包装类型
        Integer e = 100;
        Integer f = 100;

        Integer g = 200;
        Integer h = 200;

        System.out.println("包装类型类型");
        System.out.println(e==f);//true
        System.out.println(g==h);//false  警惕!!!! -128 ~ 127 之内不需要new对象,超过需要

        //new 实例
        Integer i =new Integer(100);
        Integer j =new Integer(100);

        Integer k =new Integer(200);
        Integer l =new Integer(200);

        System.out.println("包装类型类型 new实例");
        System.out.println(i==j);//false
        System.out.println(k==l); //false

        //自动拆装箱
        Integer a100 =a;
        Integer c200 =c;
        System.out.println("包装类型类型 自动拆装箱");
        System.out.println(a100==e);//true
        System.out.println(c200==h); //false -128 ~ 127 之内不需要new对象,超过需要




    }

}

打印结果

基本数据类型
true
true
包装类型类型
true
false
包装类型类型 new实例
false
false
包装类型类型 自动拆装箱
true
false
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值