Java基础笔记之包装类型的缓存机制(Byte,Short,Integer,Long,Character,Boolean)

包装类型的缓存机制


前言

Java 基本数据类型的包装类型的大部分都用到了缓存机制来提升性能。
ByteShortIntegerLong 这 4 种包装类默认创建了数值 [-128,127] 的相应类型的缓存数据,Character 创建了数值在 [0,127] 范围的缓存数据,Boolean 直接返回 True or False。而两种浮点数类型的包装类 FloatDouble 并没有实现缓存机制。


一、Byte 缓存源码:

public static Byte valueOf(byte b) {
		//偏移量offset的作用:由于byte的取值范围是[-128,127],整体向后移动128,是为了找到ByteCache.cache[]数组的索引下标。
        final int offset = 128;
        return ByteCache.cache[(int)b + offset];
    }
    
 private static class ByteCache {
        private ByteCache() {}
		//缓存数组
        static final Byte[] cache;
        static Byte[] archivedCache;

        static {
            final int size = -(-128) + 127 + 1;

            // Load and use the archived cache if it exists
            CDS.initializeFromArchive(ByteCache.class);
            if (archivedCache == null || archivedCache.length != size) {
                Byte[] c = new Byte[size];
                //从-128开始
                byte value = (byte)-128;
                //循环创建Byte对象
                for(int i = 0; i < size; i++) {
                    c[i] = new Byte(value++);
                }
                
                archivedCache = c;
            }
            cache = archivedCache;
        }
    }

二、Short 缓存源码

public static Short valueOf(short s) {
        final int offset = 128;
        int sAsInt = s;
        //判断形参 s 是否介于 [-128,127]
        if (sAsInt >= -128 && sAsInt <= 127) { // must cache
            return ShortCache.cache[sAsInt + offset];
        }
        return new Short(s);
    }
 private static class ShortCache {
        private ShortCache() {}
		//缓存数组
        static final Short[] cache;
        static Short[] archivedCache;

        static {
            int size = -(-128) + 127 + 1;

            // Load and use the archived cache if it exists
            CDS.initializeFromArchive(ShortCache.class);
            if (archivedCache == null || archivedCache.length != size) {
                Short[] c = new Short[size];
                //从-128开始
                short value = -128;
                 //循环创建Short对象
                for(int i = 0; i < size; i++) {
                    c[i] = new Short(value++);
                }
                archivedCache = c;
            }
            cache = archivedCache;
        }
    }

三、Integer 缓存源码

public static Integer valueOf(int i) {
		//判断形参 i 是否介于 [-128,127]
		//IntegerCache.low = -128,本质作用和偏移量一样
        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;
        static final Integer[] cache;
        static Integer[] archivedCache;

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

            // Load IntegerCache.archivedCache from archive, if possible
            CDS.initializeFromArchive(IntegerCache.class);
            int size = (high - low) + 1;

            // Use the archived cache if it exists and is large enough
            if (archivedCache == null || size > archivedCache.length) {				
                Integer[] c = new Integer[size];
                int j = low;
                //循环创建Integer对象
                for(int i = 0; i < c.length; i++) {
                    c[i] = new Integer(j++);
                }
                archivedCache = c;
            }
            cache = archivedCache;
            // range [-128, 127] must be interned (JLS7 5.1.7)
            assert IntegerCache.high >= 127;
        }

        private IntegerCache() {}
    }

四、Long 缓存源码

 public static Long valueOf(long l) {
 		//偏移量
        final int offset = 128;
        //判断形参 i 是否介于 [-128,127]
        if (l >= -128 && l <= 127) { // will cache
            return LongCache.cache[(int)l + offset];
        }
        return new Long(l);
    }
private static class LongCache {
        private LongCache() {}

        static final Long[] cache;
        static Long[] archivedCache;

        static {
            int size = -(-128) + 127 + 1;

            // Load and use the archived cache if it exists
            CDS.initializeFromArchive(LongCache.class);
            if (archivedCache == null || archivedCache.length != size) {
                Long[] c = new Long[size];
                //从-128开始
                long value = -128;
                //循环创建Long对象
                for(int i = 0; i < size; i++) {
                    c[i] = new Long(value++);
                }
                archivedCache = c;
            }
            cache = archivedCache;
        }
    }

五、Character 缓存源码

public static Character valueOf(char c) {
		//判断是否介于 [0,127]
        if (c <= 127) { // must cache
            return CharacterCache.cache[(int)c];
        }
        return new Character(c);
    }
 private static class CharacterCache {
        private CharacterCache(){}

        static final Character[] cache;
        static Character[] archivedCache;

        static {
        	//0-127,一共128
            int size = 127 + 1;

            // Load and use the archived cache if it exists
            CDS.initializeFromArchive(CharacterCache.class);
            if (archivedCache == null || archivedCache.length != size) {
                Character[] c = new Character[size];
                //从0开始循环创建 Character 对象
                for (int i = 0; i < size; i++) {
                    c[i] = new Character((char) i);
                }
                archivedCache = c;
            }
            cache = archivedCache;
        }
    }

六、Boolean 缓存源码

public static Boolean valueOf(boolean b) {
		//返回 TRUE 或者 FALSE
        return (b ? TRUE : FALSE);
   }

总结

缓存机制的结果是只要是在缓存区间内就返回相同的对象,也就意味着只要值相同,对象就相同。此时使用 == 判断返回 true

public class WrapperCache {
    public static void main(String[] args) {

        Integer integer = Integer.valueOf(25);
        Integer integer1 = Integer.valueOf(25);
        System.out.println(integer == integer1);   //true
        
        //自动装箱,等价于 Integer.valueOf(25)
        Integer i = 25;
        System.out.println(i == integer);   //true

        @SuppressWarnings("removal")
        Integer integer2 = new Integer(25);	//是直接创建没有走缓存
        System.out.println(i == integer2);  //false
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值