Java中 Integer包装类 常量池

Java中 Integer包装类常量池

在本文中,我们将研究一下Integer类常量池和其常量池的用途?

Integer类
Integer类作为最常用的包装类,相信大家都不陌生,那么,你了解自动装箱和手动创建实例对象间的区别么?请看下一段代码:


	public static void test1() {
		Integer num1 = new Integer(10);
		Integer num2 = new Integer(10);
		System.out.println(num1 == num2);
	}
	
	
	public static void test2() {
		Integer num1 = 10;
		Integer num2 = 10;
		System.out.println(num1 == num2);
	}

输出结果如下:

test1: false
test2: true

显然test1中应当返回false,因为我们在堆空间中创建了两个新的对象,在比较其地址值时明显不同;但是为什么test2中可以返回true呢?

我们尝试反编译这个类的Class文件,得到的类中有如下代码:

public static void test2() {
		Integer num1 = Integer.valueOf(10);
		Integer num2 = Integer.valueOf(10);
		System.out.println(num1 == num2);
	}

也就是说,当我们试图用自动装箱的方式创建一个Integer类对象时,实际上调用了Integer类的ValueOf(int i)静态方法。
让我们来看看此方法的源码:

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

再让我们看看JDK中对此方法的说明:

valueOf
public static Integer valueOf(int i)
返回一个表示指定的 int 值的 Integer 实例。如果不需要新的 Integer 实例,则通常应优先使用该方法,而不是构造方法 Integer(int),因为该方法有可能通过缓存经常请求的值而显著提高空间和时间性能。

发现通过此方法创建对象时,将会先判断创建的实例大小是否包含在缓存当中,如果包含在缓存中,直接返回缓存中的值。
此方法中引用了Integer类的内部类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 =
                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类在加载时,将会直接缓存-128~127之间数值的所有Integer类实例对象,存放在常量池中;
所以我们就清楚了,如果实参的值在-128~127之间,我们调用ValueOf方法时,它就从缓存中直接返回实例。此时我们使用==比较地址值,当然就会返回true。

**总结:当我们使用Integer i = value;的格式时,如果i是在-128到127之间,不会去堆中创建对象,而是直接返回IntegerCache中的值;如果值不在上面范围内,才会从堆中创建对象。= 会调用valueOf()方法,valueOf(int)会走缓存。

Integer i2 = new Integer(value);不管参数的value是多少都会从堆中创建对象,与IntegerCache没关系。**

看完本文章后, 相信现在您就可以正确回答出下列方法的返回值了:

    public static void test1() {
		Integer num1 = new Integer(127);
		Integer num2 = new Integer(127);
		System.out.println(num1 == num2);
	}
	
	
	public static void test2() {
		Integer num1 = 127;
		Integer num2 = 127;
		System.out.println(num1 == num2);
	}
	
	
	public static void test3() {
		Integer num1 = 128;
		Integer num2 = 128;
		System.out.println(num1 == num2);
	}
	
	public static void test4() {
		Integer num1 = new Integer(127);
		Integer num2 = 127;
		System.out.println(num1 == num2);
	}

test1: false
test2: true
test3: false
test4: false

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值