Integer.valueof()学习之享元模式

问题描述:

1. Integer类初始化

//当这样定义integer的引用初始化为3的时候,则会自动调用Integer类的valueof()方法,与Integer i=Integer.valueof(3)等价
1.Integer i=3;
//这种定义与初始化方式与上一种方式不一样,效率比上一种效率低
2.Integer i=new Integer(3);

2.Integer 类比较

public static void test() {
        Integer a1 = 3;
        Integer b1 = Integer.valueOf(3);

        Integer a2 = 200;
        Integer b2 = Integer.valueOf(200);

        if (a1 == b1) {
            System.out.println("a1=b1");
        } else {
            System.out.println("a1!=b1");
        }
        if (a2 == b2) {
            System.out.println("a2==b2");
        } else {
            System.out.println("a2!=b2");
        }

    }
 

输出结果:

a1=b1
a2!=b2
 

输出结果不一样,这是为什么呢?
我们先看一下valueof()的源代码


相关设计模式:享元模式


    public static Integer valueOf(String s) throws NumberFormatException {
        return Integer.valueOf(parseInt(s, 10));
    }


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

private static class IntegerCache {

        //设置缓存中数组元素的最小值,而不是下标
        static final int low = -128;
        //缓存的最大值
        static final int high;
        //定义一个Integer数组
        static final Integer cache[];

        static {
            // 设置缓存的最大值
            int h = 127;
            //这里静态块先不用看,他是可以通过设置jdk的AutoBoxCacheMax参数调整来获取最大值h,自动缓存区间设置为[-128,h]。
            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;

            //设置缓存的大小为256
            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                //在数组下标为0-255的数组中预先存入-128~127的元素值,这里就用到了享元模式,预先将-128-127存入数组当中,当要用的时候直接取出来
                cache[k] = new Integer(j++);

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

现在我们来解释这段代码就能看明白

//当i的值-128<i<127的时候,直接从cache数组中取出对应i值的引用(注意是引用),当i>127或者i<-128的时候,则重新创建一个Integer(i)对象
 public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

问题1的解释:

1.Integer i=3;
2.Integer i=new Integer(3);
 

当这样定义integer的引用初始化为3的时候,则会自动调用Integer类的valueof()方法,因为3在cache[256](cache[0]=-128,cache[1]=-127 ….. cache[256]=128)数组中,所以返回时直接3对应的引用,而不用new Integer(3)重新创建一个音乐,所以Integer i=3效率高

问题2解释:

与解释1原理是一样的,

        Integer a1 = 3;
        Integer b1 = Integer.valueOf(3);
        Integer a2 = 200;
        Integer b2 = Integer.valueOf(200);

即使Integer a2 = 200;调用了valueof()方法,但是200>128,此时cache数组中没有200这个值,因此只能采用new Integer()重新创建Integer对象了,这这里连续创建了两个对象,他们的引用是不一样的,因此a2!=b2(==表示的是引用)。

猜你喜欢

转载自fjding.iteye.com/blog/2311241
今日推荐