- 引言
学习java的最好方法就是看源码和官方文档
关于Integer的缓冲池IntegerCache这里从源码角度进行分析,但是Integer的缓冲池与jvm内存模型之间的关系还有待研究。
- Integer类源码分析
jdk版本:jdk1.8
public final class Integer extends Number implements Comparable<Integer> {
/*
* 内部静态类
*/
private static class IntegerCache {
//声明缓存池下界
static final int low = -128;
//声明缓存池上界
static final int high;
//声明缓存数组
static final Integer cache[];
//静态块
static {
//缓存池的上界默认为127,可以自己设置
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.