关于常量池

这里暂且把Integer作为包装类的代表来说明.

我们知道每次new都是创建一个新的对象,由于==比较的是内存地址,所以下面的代码不会为true:

        String s1 = new String("1");
        String s2 = new String("1");
        System.out.println(s1 == s2); //false

但是下面的代码可以为true,用到的就是常量池.多长的字符串都可以:

        String s3 = "1";
        String s4 = "1";
        System.out.println(s4 == s3);//true
        
        String sl1 = "qwertyuiop[]asdfghjkl;'zxcvbnm,./";
        String sl2 = "qwertyuiop[]asdfghjkl;'zxcvbnm,./";
        System.out.println(sl1 == sl2);//true

但是在Integer中,情况有所不同(new的情况下一定是false,这里不做测试):

        Integer i1 = 100;
        Integer i2 = 100;
        System.out.println(i1 == i2);//true
        Integer i3 = 192;
        Integer i4 = 192;
        System.out.println(i3 == i4);//false
        Integer i5 = -100;
        Integer i6 = -100;
        System.out.println(i5 == i6);//true
        Integer i7 = -192;
        Integer i8 = -192;
        System.out.println(i7 == i8);//false

这里利用了自动装箱的特性,我们知道自动装箱调用的是Integer.valueOf(int i),我们来看看这个方法里面的逻辑:

    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)//i在这个范围中的话.
            return IntegerCache.cache[i + (-IntegerCache.low)];//返回这样的一个值.
        return new Integer(i);//否则返回新创建的Integer(i)对象
    }

这"一个值"是什么值? 

 有个IngeterCache,cache在计算机中代表缓存的意思,那么这个东西是不是就是Integer的"缓存"?来看看源码: 

 private static class IntegerCache {
1->     static final int low = -128;
2->     static final int high;
3->     static final Integer cache[];

        static {
4->         // high value may be configured by property
5->         int h = 127;
6->         String integerCacheHighPropValue =
                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
7->          if (integerCacheHighPropValue != null) {
                try {
8->                 int i = parseInt(integerCacheHighPropValue);
9->                 i = Math.max(i, 127);
                    // Maximum array size is Integer.MAX_VALUE
10->                h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
                } catch(NumberFormatException nfe) {
                    // If the property cannot be parsed into an int, ignore it.
                }
            }
11->        high = h;

12->        cache = new Integer[(high - low) + 1];
13->        int j = low;
            for(int k = 0; k < cache.length; k++)
14->            cache[k] = new Integer(j++);
            // range [-128, 127] must be interned (JLS7 5.1.7)
15->        assert IntegerCache.high >= 127;
        }
        private IntegerCache() {}
    }

IntegerCache是Integer类中的一个私有内部类,按照标识的数字来解释:

1.low(可以理解为下限)是-128.

2.定义一个high(可以理解为上限),但是未赋值.

3.定义一个数组cache(池),尚未初始化,valueOf返回的值就是这个数组元素的类型:int.

4.可以通过配置读取high的值.

5.变量h,值为127(默认上限).

6.看起来是通过配置文件读取值.

7.读取到的值不为空,不是合法的数字就抛出数字格式异常.

8.将读取到的值转换为int.

9.转换后的值与127之中找一个较大值.

10.一个是i,一个是Ingeter最大值减去负的low(即128)再减去1,这两个值取较小值,赋值给h.

11.high赋值为h.

12.cache被初始化为容量大小为high + 128 + 1的数组(多的那个1大概是用来存储0?).

13.定义一个变量j,值为-128(low).

14.从j到high依次添加进cache.

15.这里使用了断言:只要high不符合大于等于127的条件,就退出,并抛出AssertionError.

可以看出,常量池最小的范围是-128到127,但是可以由我们自定义.如果定义的值在cache中,就直接返回int值,也就是在常量池内的两个值==为true的原因:基本数据类型直接比较.否则就返回新建的Integer对象.

下面给出Long的常量池LongCache代码,可以看出范围是固定的-128到127,不能修改,Short,Byte也是一样的.

    private static class LongCache {
        private LongCache(){}

        static final Long cache[] = new Long[-(-128) + 127 + 1]; 

        static {
            for(int i = 0; i < cache.length; i++)
                cache[i] = new Long(i - 128);
        }
    }

Double,Float没有这些代码. 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值