在上篇《java的自动拆箱会发生NPE》博客中接收了java中的Integer中的自动拆箱产生的NPE,其实对于所有的包装类来说都是一样的,都会产生这样的问题,大家需要举一反三,做学问学知识要懂得反思总结。

一、前情回顾

先回顾下上次的知识点,

自动拆箱 实际调用的是intValue()方法

自动装箱 实际调用的是valueOf(int i)方法

其他的包装类,小伙伴们自己总结

二、Integer的本地缓存

好了话不多说,书接上回,开始这次的分享,上次说到在自动装箱的时候还大有玄机,这个玄机就是本地缓存。这个玄机在自动装箱的方法中,如下

/**
* Returns an {@code Integer} instance representing the specified
* {@code int} value. If a new {@code Integer} instance is not
* required, this method should generally be used in preference to
* the constructor {@link #Integer(int)}, as this method is likely
* to yield significantly better space and time performance by
* caching frequently requested values.
*
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
*
* @param i an {@code int} value.
* @return an {@code Integer} instance representing {@code i}.
* @since 1.5
*/
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}

先看下其方法说明吧,用我大学英语六级的水平给大家翻译下,献丑了大家莫怪,

返回一个代表指定int的Integer对象,如果一个新的Integer实例不是必需的,这个方法通常使用构造方法来生成,另一方面,这个方法通常为了节省空间和实际会缓存一些经常使用的值

这个方法会缓存[-128~127]间的值,也可能会缓存这个范围以外的值

翻译的太差劲了,大体意思是如果参数在[-128,127]间则会从缓存中取,如果不在则直接生成Integer的实例。还有很重要的一点最大值可以配置。

看方法的逻辑也是这样的,看下Integer初始化缓存的代码,在Integer中有静态内部类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循环,生成缓存
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() {}
}

上面的代码加注释足以说明一切,不再一一解释了,默认情况下Integer中存在[-128,127]范围内的的256个缓存Integer实例。

三、验证本地缓存

上面说到,在Integer中存在缓存,重要的一点是在调用valueOf()方法的时候才会校验缓存,valueOf方法共有以下几个

重要声明,在使用构造方法的时候是没有缓存的,看构造方法

 /**
* Constructs a newly allocated {@code Integer} object that
* represents the specified {@code int} value.
*
* @param value the value to be represented by the
* {@code Integer} object.
*/
public Integer(int value) {
this.value = value;
}

那么在验证的时候就不能使用构造方法的方式,需要使用自动装箱的方式。

1、构造方法没有缓存

public class TestIntegerConstruct {
public static void main(String[] args) {
Integer i1=new Integer(1);
Integer i2=new Integer(1);
System.out.println(i1==i2); }
}

打印结果为,

false

Process finished with exit code 0

打印出来为false,说明i1和i2为两个不同的对象。

2、valueOf()方法才有的缓存

使用valueOf构造两个Integer对象,

public class TestValueOf {
public static void main(String[] args) {
Integer i1=1;
Integer i2=1;
System.out.println(i1==i2);
}
}

打印结果为,

true

Process finished with exit code 0

说明i1和i2为同一个实例,这里使用的是==来判断,对引用对象来说判断的不正是其地址。

多说一点,这里两个引用类型的比较,大家不要像我这里似的,使用“==”,请使用equals方法,使用“==”比较的是内存地址,在大部分情况下,内存地址肯定不相等,而使用equals方法就说不准了,equals方法比较的是什么?

3、Integer的equals方法

看下Integer的equals方法比较的是什么东西

public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}

看到没,搜先判断的是类型,然后调用其intValue方法,也就是拆箱,比较的是Integer中value的值,也就是数值的比较。

四、总结

本文,分享了Integer中的本地缓存,需要明白以下几个问题,

1、什么时候会用到本地缓存?--调用valueOf方法的时候

2、本地缓存的大小;--默认为[-128,127],最大值可设置

3、equals方法比较的是什么;--比较的是值得大小,非内存地址

往期推荐

java的自动拆箱会发生NPE

源码中的设计模式--单例模式

java的Integer中也会有缓存的更多相关文章

  1. 谈谈Integer中的静态类IntegerCache

            学习的本质就是一个赋值的过程,用新知识来覆盖你的旧知识或者无知(null).掌握知识是自己的, 分享知识,才能帮助更多的人,创造更大的价值.学贵以恒,以此自勉,与君共享.----曦阳X ...

  2. 【JDK】:java.lang.Integer源码解析

    本文对JDK8中的java.lang.Integer包装类的部分数值缓存技术.valueOf().stringSize().toString().getChars().parseInt()等进行简要分 ...

  3. Integer 中的缓存类IntegerCache

    2014年去某公司笔试的时候遇到这么一道题: public class Test { public static void main(String[] args) { Integer int1 = I ...

  4. Java之戳中痛点 - (7)善用Java整型缓存池

    先看一段代码: package com.test; import java.util.Scanner; public class IntegerCache { public static void m ...

  5. Java自动装箱中的缓存原理

    今天看到一道'经典'面试题: Integer a = 100; Integer b = 100; System.out.println(a==b); Integer a2 = 200; Integer ...

  6. 对Integer类中的私有IntegerCache缓存类的一点记录

    对Integer类中的私有IntegerCache缓存类的一点记录 // Integer类有内部缓存,存贮着-128 到 127. // 所以,每个使用这些数字的变量都指向同一个缓存数据 // 因此可 ...

  7. Java Integer中的IntegerCache小记

      同事今天给我发了一个关于Java拆装箱的ppt,里面有这么一段代码 Integer c = Integer.valueOf(3); Integer d = Integer.valueOf(3); ...

  8. Integer 中的缓存类 IntegerCache

    我们先看一段代码: public class TestAutoBoxing { public static void main(String[] args) { //-128到127之间 Intege ...

  9. Java.lang.Integer类中toString(int i, int radix)的具体实现

    Java.lang.Integer.toString(int i,int radix)方法可以实现将一个int类型的10进制的数据转换为指定进制的数据. api文档中介绍: 返回第二个参数指定的基数中 ...

随机推荐

  1. 前端知识之css样式

    前端之CSS样式 css介绍 css是为html标签设置样式的 css由选择器和声明组成 声明包括属性和属性值 声明之间用分号:隔开 css注释 /注释类容/ css的几种引入方式 行内样式 不推荐使 ...

  2. Flask 之路由系统

    Flask中的路由系统其实我们并不陌生了,从一开始到现在都一直在应用 @app.route("/",methods=["GET","POST" ...

  3. 6月11日 python学习总结 框架理论

    Web框架本质及第一个Django实例   Web框架本质 我们可以这样理解:所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端. 这样我们就可以自己实现Web ...

  4. Linux安全加固手册

    1      身份鉴别 1.1         密码安全策略 操作系统和数据库系统管理用户身份鉴别信息应具有不易被冒用的特点,口令应有复杂度要求并定期更换. 设置有效的密码策略,防止攻击者破解出密码 ...

  5. Spring Cloud Alibaba分布式事务组件 seata 详解(小白都能看懂)

    一,什么是事务(本地事务)? 指作为单个逻辑工作单元执行的一系列操作,要么完全地执行,要么完全地不执行. 简单的说,事务就是并发控制的单位,是用户定义的一个操作序列.      而一个逻辑工作单元要成 ...

  6. go语言学习入门篇1---go语言的主要特性与发展

    1.1 影响 Go 语言发展的早期编程语言 正如 "21 世纪的 C 语言" 这句话所说,Go 语言并不是凭空而造的,而是和 C++.Java 和 C# 一样属于 C 系.不仅如此 ...

  7. k8s集群搭建过程详解

    准备工作 安装CentOS7虚拟机 略 安装Docker 略 关闭CentOS7自带的防火墙服务 systemctl disable firewalld systemctl stop firewall ...

  8. XML常用解析API有哪几种?

    XML常用解析API有JAXP.JDOM.Dom4j等. JAXP是Java API for XML Processing的英文字头缩写,中文含义是:用于XML文档处理的使用Java语言编写的编程接口 ...

  9. python 函数基础知识

    1.函数返回的多个值会被组织成元组被返回,也可以用多个值来接收 2.调用函数时候,传入的参数叫实际参数,简称实参,定义函数的参数叫做形式参数,简称形参-- 位置参数 def mymax(x,y): a ...

  10. 学习SVN02

    代码发布方案: 1,安装,优化 软件环境,(nginx,lvs)  <-------运维工程师 2,程序代码(不断更新).   <--------开发工程师,(开发,运维都可以发布) 3, ...