Integer比较值面试题总结
发布日期:2021-05-07 11:59:36 浏览次数:26 分类:技术文章

本文共 2959 字,大约阅读时间需要 9 分钟。

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
上一篇:并查集
下一篇:TCP/IP三次握手和四次挥手粗解

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2025年03月26日 20时21分59秒