Java常量池简单测试

我们大家都知道,java中的常量池技术,是为了方便快捷地创建某些对象而出现的,因此方便理解写了下面一段代码来简单介绍常量池的作用:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    while (input.hasNextInt()) {
        int ii = input.nextInt();
        System.out.println("====" + ii + "的相等判断=====");
        //两个通过new产生的Integer对象
        Integer i = new Integer(11);
        Integer j = new Integer(11);
        System.out.println("new产生的对象:" + (i == j));
        //基本类型转为包装类型后比较
        i = ii;
        j = ii;
        System.out.println("基本类型转换的对象:" + (i == j));
        //通过静态方法生成一一个实例
        i = Integer.valueOf(ii);
        j = Integer.valueOf(ii);
        System.out.println("valueOf产生的对象:" + (i == j) + "\n");
    }
}

运行程序,输入几个数字测试结果:

127
====127的相等判断=====
new产生的对象:false
基本类型转换的对象:true
valueOf产生的对象:true

128
====128的相等判断=====
new产生的对象:false
基本类型转换的对象:false
valueOf产生的对象:false

455
====455的相等判断=====
new产生的对象:false
基本类型转换的对象:false
valueOf产生的对象:false

为什么会出现这样的结果?其实原理很简单,我们来解释一下

(1)new产生的对象
使用new声明的就是要生成一个新的对象,两个对象地址肯定不等,使用“==”判断,没得说,结果为false。

(2)装箱生成的对象
首先要说明的是装箱动作是通过valueOf方法实现的。也就是说两个对象的装箱算法是相同的,那么结果值肯定也是相同的。于是我们可以查看一下Integer.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.low到IntegerCache.high之间的int类型转换成Integer时对象时,直接从IntegerCache.cache数组中获得。由下面源码我们可以知道low、high、cache的值是什么东西,也就是说cache数组容量是-128和127之间。如果int参数在- 128和127之间,则直接从整型池中获得对象,不在该范围的int类型则通过new生成包装对象。

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() {}
}

明白以上两点,要理解输出结果就很容易了。输入127,刚好这个值在IntegerCache.cache数组中,因此包装对象从整型常量池中获得,获得的对象都是同一个,所以地址肯定也相同。而超过IntegerCache.cache数组范围获得的对象都是通过new产生一个新的对象,地址不同,使用“==”比较也就不相等了。

总结:使用常量池即提交了系统性能,也节约了内存空间。当使用valueOf生成对象时,要注意这个问题,顺便提醒下,比较两个对象值是否相等时,最好使用equals方法,避免出现使用“==”产生非预期结果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值