int和Integer的区别

说明

面试时被问到int和Integer的区别,只回答了自动拆箱装箱,回去后查找了有关资料,在这里总结下

正文

1. 类型不同

int为java的基本数据类型,Integer是int的包装类,引用类型

2. 默认值不同

int的默认值为0,Integer为null
写javabean时,遇到数据类型一般写Integer。如:用int表示学生的考试成绩(用int不合适,这里只做例子),当学生因为某种原因没有参加考试,当用int时,不赋值int默认为0,那表示学生考试成绩为0,会出现歧义,使用Integer包装类默认值为null,可以清楚的表示学生没有参加考试

3.声明变量时不同

声明Integer变量时必须进行实例化,new一个对象,在堆中开辟内存空间,创建一个引用指向该内存地址。而声明int时可以直接赋值,不需要实例化

4.拆箱装箱

装箱,就是把基本类型用对应的引用类型包装起来,使他们具有对象的特质
拆箱,就是把引用类型包装的值用基本类型数据表示
在J2SE5.0后提供了自动装箱和拆箱的功能

Integer i = 100;//自动装箱

Integer a = new Integer(200);
int b = a; // 自动拆箱

5.int和Integer比较

public static void main(String[] args) {
        int a = 100;
        Integer b = new Integer(100);
        System.out.println(a == b);

        Integer c = 127;
        Integer d = 127;
        System.out.println(c == d);


        Integer e = 128;
        Integer f = 128;
        System.out.println(e ==f);

        Integer g = new Integer(120);
        Integer h = new Integer(120);
        System.out.println(g == h);

    }

输出结果为
true
true
false
false

==符号比较两个值是否相等
第一个 进行了自动拆箱 比较值相等 返回true
第二个 和 第三个 都是声明的引用类型的值,进行了自动装箱,在java编译时 Integer c = 127;被翻译成了Integer c = Integer.valueOf(100);d,e,f同样如此,可是为什么c和d的比较结果与e和f的比较结果不同? 关键就在Integer.valueOf();方法上

public static Integer valueOf(int i) {
        assert IntegerCache.high >= 127;
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
}

private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[];

        static {
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
                int i = parseInt(integerCacheHighPropValue);
                i = Math.max(i, 127);
                // Maximum array size is Integer.MAX_VALUE
                h = Math.min(i, Integer.MAX_VALUE - (-low));
            }
            high = h;

            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);
        }

        private IntegerCache() {}
}

通过源码我们可以看到,Integer类中有个静态内部类IntegerCache,使用Integer数组缓存-128到127的Integer对象,当使用valueOf()方法时,判断如果值的范围在-128-127之间时,就会返回数组中的对象,否则就创建一个新的对象。所以 c 和 d 属于同一对象,返回true,而e 和 f是两个不同的对象,返回false。


    /**
     * Cache to support the object identity semantics of autoboxing for values between
     * -128 and 127 (inclusive) as required by JLS.
     *
     * The cache is initialized on first usage.  The size of the cache
     * may be controlled by the -XX:AutoBoxCacheMax=<size> option.
     * During VM initialization, java.lang.Integer.IntegerCache.high property
     * may be set and saved in the private system properties in the
     * sun.misc.VM class.
     */

通过该类的注释可知,缓冲在第一次被使用的时候被初始化,并且缓冲的大小可由虚拟机参数-XX:AutoBoxCacheMax设置

第四个 显示创建了两个不同的对象,结果为false

参考文章:
JAVA包装类及其拆箱装箱及Integer类拆装箱的细节
Integer和int的区别

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值