美文网首页
Java--包装类的缓存问题

Java--包装类的缓存问题

作者: 李赫尔南 | 来源:发表于2022-10-03 09:28 被阅读0次

  整型、char类型所对应的包装类,在自动装箱时,对于-128~127之间的值会进行缓存处理,其目的是提高效率。
  缓存处理的原理为:如果数据在-128~127这个区间,那么在类加载时就已经为该区间的每个数值创建了对象,并将这256个对象存放到一个名为cache的数组中。每当自动装箱过程发生时(或者手动调用valueOf()时),就会先判断数据是否在该区间,如果在则直接获取数组中对应的包装类对象的引用,如果不在该区间,则会通过new调用包装类的构造方法来创建对象。
  下面我们以Integer类为例,看一看Java为我们提供的源码,加深对缓存技术的理解,如示例1所示。
【示例1】Integer类相关源码如下:

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

  这段代码中我们需要解释下面几个问题:
  1. IntegerCache类为Integer类的一个静态内部类,仅供Integer类使用。
  2. 一般情况下 IntegerCache.low为-128,IntegerCache.high为127,IntegerCache.cache为内部类的一个静态属性,如示例2所示。
【示例2】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 = 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() {}
}

  由上面的源码我们可以看到,静态代码块的目的就是初始化数组cache的,这个过程会在类加载时完成。
  下面我们做一下代码测试,如示例3所示。
【示例3】测试代码

public class Test {
    public static void main(String[] args) {
        Integer in1 = -128;
        Integer in2 = -128;
        System.out.println(in1 == in2);//true 因为123在缓存范围内        
        System.out.println(in1.equals(in2)); //true
        Integer in3 = 1234;
        Integer in4 = 1234;
        System.out.println(in3 == in4);//false 因为1234不在缓存范围内
        System.out.println(in3.equals(in4); //true
    }
}

输出:
  true
  true
  false
  true

相关文章

  • Java--包装类的缓存问题

      整型、char类型所对应的包装类,在自动装箱时,对于-128~127之间的值会进行缓存处理,其目的是提高效率。...

  • Java--包装类的用途

    对于包装类来说,这些类的用途主要包含两种:  1. 作为和基本数据类型对应的类型存在,方便涉及到对象的操作,如Ob...

  • Java--包装类基本知识

      Java是面向对象的语言,但并不是“纯面向对象”的,因为我们经常用到的基本数据类型就不是对象。但是我们在实际应...

  • 从零开始的JDK之旅 day03

    包装类(类) 常见问题: Integer有个内部类,维护了从[-128~127(最大值可调)]的缓存区,虚拟机一开...

  • Integer包装类转换

    intvalue把包装类转成基本数据类型, parseInt包字符串转成对应的包装类 valueOf(高速缓存)

  • Java基础——包装类缓存

    在开始之前,我们先来看一下Java语言规范(JLS7-5.1.7)中的一小段内容: If the value p ...

  • JDK 源码解析 —— Integer

    说到Java中的Integer包装类,大家或多或少的都听说过Integer缓存,下面我们先从Integer缓存相关...

  • 优秀博客集锦(一)

    理解Java Integer的缓存策略Java中Integer和其他包装类具有缓存机制,会缓存常用的一部分数字。 ...

  • Java--类

    类的定义方式   上面的类定义好后,没有任何的其他信息,就跟我们拿到一张张图纸,但是纸上没有任何信息,这是一个空类...

  • java Integer包装类之自动装拆箱 + Integer

    java Integer包装类之自装拆箱 + Integer == (缓存池)面试题 Integer 自动装拆箱 ...

网友评论

      本文标题:Java--包装类的缓存问题

      本文链接:https://www.haomeiwen.com/subject/npdsartx.html