Java源码解读扫盲【Integer】

一、类的描述

Integer 是基本数据类型int的封装类,用来表示基本数据类型int的面向对象的类,类中包含了原始类型int的值以及int操作的方法

二、源码解析

public final class Integer extends Number implements Comparable<Integer> {
//Integer的值
private final int value;

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

//由字符串来初始化Integer 默认转换为10进制
public Integer(String s) throws NumberFormatException {
    this.value = parseInt(s, 10);
}
public static int hashCode(int value) {
    return value;
}
public boolean equals(Object obj) {
    if (obj instanceof Integer) {
        return value == ((Integer)obj).intValue(); //比较两个Integer的int值
    }
    return false;
}
public int compareTo(Integer anotherInteger) {
    return compare(this.value, anotherInteger.value);
}

public static int compare(int x, int y) {
    return (x < y) ? -1 : ((x == y) ? 0 : 1); //比较x,y的大小,x<y返回-1,x=y返回0,x>y返回1
}
//内部字符缓存类
private static class IntegerCache {
    //缓存的下界,-128,不可变  
    static final int low = -128;
    //缓存上界,暂为null
    static final int high;
    static final Integer cache[]; //利用数组来缓存

    static {
        // high value may be configured by property
        // 缓存上届,可以通过JVM属性来配置
        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) -1);
        }
        high = h;

        //获取Integer中所有能保存的数据
        cache = new Integer[(high - low) + 1];
        int j = low;
        //缓存所有Integer的数据
        for(int k = 0; k < cache.length; k++)
            cache[k] = new Integer(j++);
    }

    private IntegerCache() {}
}

这是一个内部静态类,该类只能在Integer这个类的内部访问,这个类在初始化的时候,会去加载JVM的配置,如果有值,就用配置的值初始化缓存数组,否则就缓存-128到127之间的值。

public static Integer valueOf(int i) {
    assert IntegerCache.high >= 127; //断言Integer缓存中的上届必须大于或等于127
    if (i >= IntegerCache.low && i <= IntegerCache.high) //如果i在Integer缓存中,则直接取出
        return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i); //否则,直接创建一个实例
}

比如:Integer a = 127 的执行过程,首先调用Integer.valueOf(127) ,由于127在-128到127之间(看上面的源码),所以返回一个缓存值 return IntegerCache.cache[127+128];也就是数组中下标是255的值127

三、相等判断

关于 == 的运行结果,只要我们记住一条:他比较的是引用(地址) 就好了,按照这个来分析结果

当且仅当比较的两个引用指向同一对象才返回true

举例

Integer a = new Integer(100);
Integer b = new Integer(100);
int c=100;
Integer d = 100;
Integer e = 128;
Integer f = 128;
System.out.println("a==b:"+(a==b));
System.out.println("a==c:"+(a==c));
System.out.println("a==d:"+(a==d));
System.out.println("c==d:"+(c==d));
System.out.println("e==f:"+(e==f));

运行结果

a==b:false
a==c:true
a==d:false
c==d:true
e==f:false

Integer是int的封装对象,两个对象==比较的是栈的值。
Integer a = new Integer(100);
Integer b = new Integer(100);
a与b在栈中,存的是Integer对象在堆中的地址,而不是值,a、b指向堆中的地址显然不同所以 a==b 为false

int c = 100;

因为int为值类型,引用类型Integer与值类型int比较显然比较的是值。
因为int在堆中是不开辟内存的,他在栈中的值则为他本身的值
所以a==c比较的是他们各自的value,Integer对象会自动拆箱, a==c为true

Integer d = 100;

扫描二维码关注公众号,回复: 114917 查看本文章

c和d的标记同a和c的比较,c==d为true

Integer e = 128;

Integer f = 128;

实际就是调用Interger.valueOf(100),通过上面源码知道首先判断100是否在-128-127 之间,如果是直接return 已经存在的对象,否则就只能new 一个了,所以 e==f为false

猜你喜欢

转载自my.oschina.net/u/3737136/blog/1634196