一道有关Integer和int的面试题

一道有关Integer和int的面试题

题目描述

public class test
{
    public static void main(String[] args)
    {
        Integer i1 = 50;
        int i2 = 50;
        Integer i3 = Integer.valueOf(50);
        Integer i4 = new Integer(50);
        System.out.println("i1 == i2: " + (i1 == i2));
        System.out.println("i1 == i3: " + (i1 == i3));
        System.out.println("i2 == i3: " + (i2 == i3));
        System.out.println("i3 == i4: " + (i3 == i4));
    }
}

结果如下:

i1 == i2: true
i1 == i3: true
i2 == i3: true
i3 == i4: false

分析

Integer.valueOf()

Integer i3 = Integer.valueOf(50);

对于这种声明方式,我们需要从其源码进行分析

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

首先,Valueof()会判断 传入的值 是否超过一定范围

  • 如果没有超过该范围,那么直接返回 已创建的数组 中的对象
  • 如果超过了,那么将重新创建一个Integer对象

那么,这个范围是多少呢?我们需要继续探究其源码

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)
        {
            try
            {
                int i = parseInt(integerCacheHighPropValue);
                i = Math.max(i, 127);
                // Maximum array size is Integer.MAX_VALUE
                h = Math.min(i, Integer.MAX_VALUE - (-low) - 1);
            }
            catch (NumberFormatException nfe)
            {
                // If the property cannot be parsed into an int, ignore it.
            }
        }
        high = h;

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

        // range [-128, 127] must be interned (JLS7 5.1.7)
        assert Integer.IntegerCache.high >= 127;
    }

    private IntegerCache()
    {
    }
}

通过以上源码,我们可以发现,当传入的数字在/-128(IntegerCache.low)~127(IntegerCache.high)/之间时,将直接返回在静态代码块中创建的 数组中 的 已存在的对象 (该数组在第一次调用时就被创建,在一次程序运行时,只会创建一次,其中存入了-128-127之间的Integer对象)

new Integer()

Integer i4 = new Integer(50);
private final int value;

public Integer(int value) 
{
    this.value = value;
}

通过源码,我们可以看到,这个操作调用了构造方法,将其赋给了一个 Integer 的 final 的局部变量 value

Integer i1 = 50

这个我们无法通过代码或者源码直接看到,只能通过反编译手段进行分析

image-20211031002407203

经过反编译,我们发现,Integer i1 = 50其实还是调用了Integer.valueOf方法

即 Integer i1 = 50 相当于 Integer i1 = Integer.valueOf(50)

int i2=50

在用Integer类型的i1和int类型的i2进行 == 比较的时候, 通过反编译我们可以发现,其实是使用 包装类的方放进行比较(即自动拆箱)i1.intValue() == i2

image-20211031002826137

总结

通过上面分析,我们可以得到以下

  • System.out.println("i1 == i2: " + (i1 == i2));
    System.out.println("i1 == i3: " + (i1 == i3));
    System.out.println("i2 == i3: " + (i2 == i3));
    

    对于 i1 == i2 来说,在判断时,首先会执行 Integer i1=Integer.valueOf(50);

    接着使用i1.intValue()转换成了基本数据类型50,即int i1=50

    所以题目相当于问 int i1=50 int i2=50 问i1==i2

    即比较两个基本类型,这个问题的答案显而易见是true

    由于i1相当于i3,不再进行分析

System.out.println("i3 == i4: " + (i3 == i4));

对于i3==i4,在判断时,i3对象是缓冲数组中的已存在的对象,而i4的对象是 new 的新Integer对象,所以不同。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值