当前位置: 首页> 房产> 建筑 > java包装类型缓存简单探究-Integer为例

java包装类型缓存简单探究-Integer为例

时间:2025/7/9 12:10:14来源:https://blog.csdn.net/qq_42939279/article/details/140611730 浏览次数:0次

文章目录

  • 包装类型缓存
  • 自动装箱与valueOf
  • 感悟
  • 结语

包装类型缓存

包装类型缓存是什么
本文以常用的Integer包装类为例做一个探索,感兴趣可以用类似方法查看其他包装类。
我们都知道它会缓存 -128到127之间的整数Integer对象。
结论大伙都知道。那么我们今天就来探究一下底层是怎么做的。

自动装箱与valueOf

我们都知道包装类会使用valueOf()方法自动装箱
Integer a = 10;实质上在编译成class文件后是使用了valueOf()方法生成一个Integer对象
当我想调试进入这个函数发现会直接完成这个语句。而不是进入valueOf
所以就想到了反编译。我们来看看实际的java字节码指令是什么样的

public class IntegerTest {public static void main(String[] args) {Integer a = 10;}
}

上面是执行的程序
我们用javap指令进行反编译拿到字节码指令和汇编代码。
这里我是用idea直接运行了这段代码。
target/classes目录下会生成class文件。

在这里插入图片描述
这就是java的字节码文件,我们使用反编译命令javap
控制台到该class文件目录,javap -c IntegerTest.class 可以生成
-c参数表示对代码进行反汇编。
输出如下:

 Compiled from "IntegerTest.java"
public class IntegerTest {public IntegerTest();Code:0: aload_01: invokespecial #1                  // Method java/lang/Object."<init>":()V4: returnpublic static void main(java.lang.String[]);Code:0: bipush        10          2: invokestatic  #2                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;5: astore_16: return
}

上半部分是默认的构造函数
我们直接看后半部分,我们的main函数
bipush 10,是把我们赋的值压入操作数栈
invokestatic 调用Integer.valueOf(int)方法,将操作数栈顶的int值转换为Integer对象
astore_1表示把刚才的Integer对象存储在局部变量表的索引1位置
我有用javap -v 查看了一下局部变量表
在这里插入图片描述
即把valueOf生产的Integer对象由我们声明的变量引用。

同时我们可以看到中间调用静态方法的注释

// Method java/lang/Integer.valueOf:(I)Ljava/lang/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的直接使用IntegerCache中的对象
我们来看看这个静态内部类 IntegerCache(不看也无所谓,简单来说就是IntegerCache这个类预置好了缓存的范围,通过静态代码块完成对Integer对象的缓存。默认下限-128,上限127,此范围内的Integer会存放在IntegerCacheInteger数组中)

private static class IntegerCache {static final int low = -128;static final int high;static final Integer cache[];static {// high value may be configured by propertyint 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_VALUEh = 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() {}}

由于类加载是懒加载的,内部类加载与外部类加载没有直接关系,仅在使用到时才加载(创建实例 new,调用其方法,使用该类声明等,这个jvm有严格规范。感兴趣可以搜搜)。
所以当我们Integer直接赋值一个缓存区间内的数,调用valueOf方法时就会去integerCache中获取(触发该类的加载,加载过程中会执行静态代码块,即完成缓存数组中对象的创建)。

PS:
IntegerCache代码中可以看到,缓存区间的下限low是定好的,而上限high则是可以通过虚拟机参数调节的。

感悟

源码的注释里提到,缓存是为了提供性能。
这是一个比较重要的思想。我觉得从中可以抽象出两点。
将常用的东西做一个复用,不去重复创建。
同时也提前把常用的创建好,不用等到用时创建,提高响应速度。
即提前热点缓存和重复对象复用。

结语

之前看过不少文章科普这种基础知识,看过一些源码阅读的文章。
这次自己写一篇也算自己阅读源码了,虽然是比较简单的。

这两天才回家事比较多,没有写博客,今天开始恢复更新。

感谢您的阅读,欢迎批评指正。

关键字:java包装类型缓存简单探究-Integer为例

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: