Integer比较值面试题总结

Integer比较值面试题总结

代码:

public static void main(String[] args) {
		int a = 127;
		Integer a1 = 127;
		Integer a2 = 127;
		System.out.println(a == a1);//true
		System.out.println(a == a2);//true
		System.out.println(a1 == a2);//true
		Integer a3 = 128;
		Integer a4 = 128;
		Integer a5 = new Integer(128);
		System.out.println(a3 == a4);//false
		System.out.println(a3 == a5);//false
		
		System.out.println(a1.equals(a));//true
		System.out.println(a1.equals(a2));//true
		System.out.println(a3.equals(a4));//true
		
		
	}

解析

上面代码包含的知识点有:

  1. ==与equals的区别;
  2. Integer.valueOf()源码;
  3. 基本数据类型的拆箱和装箱;

  首先,==比较的是两个对象的地址值(基本数据类型比较的是值,对象比较的是地址值);在没有重写equals方法的情况下,equals方法和 ==方法作用一致,如果重写了equals方法,那么根据重写的代码会实现不同的功能,例如String类就重写了equals方法(比较的是两个对象的值),还有包装类等等:

String s1 = new String("ab");
String s2 = new String("ab");
System.out.println(s1.equals(s2));//true

  其次,关于Integer.valueOf()的源码分析:

valueOf()源码:

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

IntegerCache源码:

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 IntegerCache.high >= 127;
        }

        private IntegerCache() {}
    }

  如下代码:

		Integer a1 = 127;
		Integer a2 = 127;
		System.out.println(a == a1);//true
		System.out.println(a == a2);//true
		System.out.println(a1 == a2);//false

  127是基本数据类型,Integer a1 = 127;等同于Integer a1 = Integer.valueOf(127);通过上面的源码得知,如果Integer.valueOf(int number) 该number的范围在[-128,127]之间,那么该值会存储在IntegerCache.cache[]数组中,而不重新进行分配空间;所以a1 == a2;
  如果该值超过该范围,例如:

		Integer s3 = 128;
		Integer s4 = 128;
		System.out.println(s3 == s4);//false

运行结果为false,堆会为其重新分配内存空间。**注明:**对于Integer数据类型,[-128,127]数据存储在方法区,其他的数据存储在堆中。

  自动拆箱装箱,也就是说当基本数据类型和包装类型进行比较的时候,包装数据类型会进行自动拆箱;所以相当于基本数据类型比较值是否相等。
  凡是两个经过new的对象进行比较,都是false;例如:

Integer a1 = new Integer(12);
Integer a2 = new Integer(12);
System.out.println(a1 == a2);//false
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值