10.int和Integer的区别(重点)

是不是觉得这个很简单?那不如先来一个题目吧!

根据下面代码写出输出结果:

Integer i1 = new Integer(12);
Integer i2 = new Integer(12);
System.out.println(i1 == i2);

Integer i3 = 126;
Integer i4 = 126;
int i5 = 126;
System.out.println(i3 == i4);
System.out.println(i3 == i5);

Integer i6 = 128;
Integer i7 = 128;
int i8 = 128;
System.out.println(i6 == i7);
System.out.println(i6 == i8);

可以运行一下看一下结果,和你想的是不是一样!

原因分析:

Integer i1 = new Integer(12);
Integer i2 = new Integer(12);
System.out.println(i1 == i2);

相信这个不难,应该答对了!

一但看到 new 这个关键字,是一定会在内存中开辟一块新内存用来存放对象的。所以这个题根本都不用看值是不是一样,直接就是 false。

来看第二部分

Integer i3 = 126;
Integer i4 = 126;
int i5 = 126;
System.out.println(i3 == i4);
System.out.println(i3 == i5);

这里先说 i3 == i5 的比较,因为一个是 int 一个是 Integer 类型,所以这时候会对 Integer 类型的变量进行拆箱操作,然后在跟 int 类型的变量进行比较。所以这时候其实是两个int类型的比较,结果是 true。

i3 == i4 和下边那部分一块分析。

第三部分

Integer i6 = 128;
Integer i7 = 128;
int i8 = 128;
System.out.println(i6 == i7);
System.out.println(i6 == i8);

i6 == i8 的原因在上边第二部分分析过了,所以这里不再说明。

主要说明 i6 == i7 和 上边的 i3 == i4 的原因。

在对Integer类型的变量通过字面量赋值的时候(Integer i = 1 ,这种形式就是字面量赋值),在底层会调用 Integer.valueOf 方法。而Integer类的内部维护了一个 Integer类型的 -128 ~ 127 的数组,如果赋值的在这个范围内,会直接使用这里边的对象。不在这个范围的数字,则会通过new 的方式新建一个对象。

看到这里应该知道为什么会产生这样的结果了吧!

最后放一下Integer类中的 缓冲数组源码:

这里是valueOf方法

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

这里看到,如果 i 满足条件 i >= IntegerCache.low && i <= IntegerCache.high 就会返回
IntegerCache.cache[i + (-IntegerCache.low)],返回 cache数组中的一个元素。
下边揭秘关键部分。 这里只拉取部分代码。

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;
            // ...
            high = h;

到这里就可以看出来范围是 -128 ~ 127 之间了,在这个范围会返回数组中已有的元素,不在这个范围就新建一个。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值