热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Boolean、Byte、Short、Integer、Long、Character的缓存机制

2019独角兽企业重金招聘Python工程师标准装箱都是执行valueOf方法:如果有缓存将判定是否在缓存范围内,否则new。拆箱则是执行xxxV

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

装箱都是执行valueOf方法:如果有缓存将判定是否在缓存范围内,否则new。

拆箱则是执行xxxValue方法!

public static boolean isSuspend(Double code) {return code == (int) 1;}public static void main(String[] args) {isSuspend(2d);}

入参先做装箱处理Double.valueOf;再做拆箱处理Double.doubleValue。
说明:拆箱与当前自己的包装类有关!

Java 语言规范中的缓存行为

在 Boxing Conversion 部分的Java语言规范(JLS)规定如下:

详情可见: https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.1.7 下的 【5.1.7. Boxing Conversion】、【5.1.8. Unboxing Conversion】

if the value p being boxed is an integer literal of type int between -128 and 127 inclusive (§3.10.1), or the boolean literal true or false (§3.10.3), or a character literal between '\u0000' and '\u007f' inclusive (§3.10.4), then let a and b be the results of any two boxing conversions of p. It is always the case that a == b.
如果一个变量 p 的值属于:-128至127之间的整数,true 和 false的布尔值,’u0000′ 至 ‘u007f’ 之间的字符中时,将 p 包装成 a 和 b 两个对象时,可以直接使用 a == b 判断 a 和 b 的值是否相等。

A boxing conversion may result in an OutOfMemoryError if a new instance of one of the wrapper classes (BooleanByteCharacterShortIntegerLongFloat, or Double) needs to be allocated and insufficient storage is available.

5.1.7 章节阐述: 

  1. 当内存空间不足以分配时,出现OutOfMemoryError; 
  2. 对于除 float、double外的的基本数据类型在自动装箱时有缓存Cache; 
  3. 在float、double装箱时,需要关注是否是NaN(Not-a-Number)

5.1.8 章节在关于拆箱的描述中: 如果为null时,将抛出NullPointerException.

Double:/*** A constant holding a Not-a-Number (NaN) value of type* {@code double}. It is equivalent to the value returned by* {@code Double.longBitsToDouble(0x7ff8000000000000L)}.*/public static final double NaN = 0.0d / 0.0;Float:/*** A constant holding a Not-a-Number (NaN) value of type* {@code float}. It is equivalent to the value returned by* {@code Float.intBitsToFloat(0x7fc00000)}.*/public static final float NaN = 0.0f / 0.0f;

eg.

Integer aa = 127, bb = 127;System.out.println(aa == bb); //true aa = 128;bb = 128;System.out.println(aa == bb); //false --因为128超出范围System.out.println(aa.equals(bb)); //true Integer a = 10; //this is autoboxingInteger b = Integer.valueOf(10); //under the hoodSystem.out.println(a == b); // trueFloat c = 0f / 0f;Float d = 0f;System.out.println(c == d); // false -- c是NaNSystem.out.println(c.isNaN()); // trueSystem.out.println(d.isNaN()); // falseDouble e = 0d / 0d;Double f = 0d;System.out.println(e == f); // false -- e是NaNSystem.out.println(e.isNaN()); // trueSystem.out.println(f.isNaN()); // false

Integer

在 Java 5 中,为 Integer 的操作引入了一个新的特性,用来节省内存和提高性能。整型对象在内部实现中通过使用相同的对象引用实现了缓存和重用。上面的规则适用于整数区间 -128 到 +127
这种 Integer 缓存策略仅在自动装箱(autoboxing)的时候有用,使用构造器创建的 Integer 对象不能被缓存。
Java 编译器把原始类型自动转换为封装类的过程称为自动装箱(autoboxing),这相当于调用 valueOf 方法!

Integer a = 10; //this is autoboxing
Integer b = Integer.valueOf(10); //under the hood

源码如下:

在-128到127区间将取缓存中的,超出范围后会new新的地址堆对象。

/*** Returns an {&#64;code Integer} instance representing the specified* {&#64;code int} value. If a new {&#64;code Integer} instance is not* required, this method should generally be used in preference to* the constructor {&#64;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.** &#64;param i an {&#64;code int} value.* &#64;return an {&#64;code Integer} instance representing {&#64;code i}.* &#64;since 1.5*/public static Integer valueOf(int i) {if (i >&#61; IntegerCache.low && i <&#61; IntegerCache.high)return IntegerCache.cache[i &#43; (-IntegerCache.low)];return new Integer(i);}

IntegerCache

 Integer 类中一个私有的静态类。

Javadoc 详细的说明这个类是用来实现缓存支持&#xff0c;并支持 -128 到 127 之间的自动装箱过程。最大值 127 可以通过 JVM 的启动参数 -XX:AutoBoxCacheMax&#61;size 修改。 缓存通过一个 for 循环实现。从小到大的创建尽可能多的整数并存储在一个名为 cache 的整数数组中。这个缓存会在 Integer 类第一次被使用的时候被初始化出来。以后&#xff0c;就可以使用缓存中包含的实例对象&#xff0c;而不是创建一个新的实例(在自动装箱的情况下)。

实际上在 Java 5 中引入这个特性的时候&#xff0c;范围是固定的 -128 至 &#43;127。后来在 Java 6 中&#xff0c;最大值映射到 java.lang.Integer.IntegerCache.high&#xff0c;可以使用 JVM 的启动参数设置最大值。这使我们可以根据应用程序的实际情况灵活地调整来提高性能。是什么原因选择这个 -128 到 127 这个范围呢&#xff1f;因为这个范围的整数值是使用最广泛的。 在程序中第一次使用 Integer 的时候也需要一定的额外时间来初始化这个缓存。

/*** Cache to support the object identity semantics of autoboxing for values between* -128 and 127 (inclusive) as required by JLS.** The cache is initialized on first usage. The size of the cache* may be controlled by the {&#64;code -XX:AutoBoxCacheMax&#61;} option.* During VM initialization, java.lang.Integer.IntegerCache.high property* may be set and saved in the private system properties in the* sun.misc.VM class.*/private static class IntegerCache {static final int low &#61; -128;static final int high;static final Integer cache[];static {// high value may be configured by propertyint h &#61; 127;String integerCacheHighPropValue &#61;sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");if (integerCacheHighPropValue !&#61; null) {try {int i &#61; parseInt(integerCacheHighPropValue);i &#61; Math.max(i, 127);// Maximum array size is Integer.MAX_VALUEh &#61; Math.min(i, Integer.MAX_VALUE - (-low) -1);} catch( NumberFormatException nfe) {// If the property cannot be parsed into an int, ignore it.}}high &#61; h;cache &#61; new Integer[(high - low) &#43; 1];int j &#61; low;for(int k &#61; 0; k &#61; 127;}private IntegerCache() {}}

缓存

Byte&#xff0c;Short&#xff0c;Long、Integer的缓存有固定范围: -128 到 127&#xff0c;对于 Character缓存范围是 0 到 127除了 Integer 可以通过参数改变范围外&#xff0c;其它的都不行

tip1: 
    String  valueOf方法只有对boolean的处理返回字面量&#xff08;字符串常量池&#xff09;&#xff0c; 其他都是new新的&#xff01;

    public static String valueOf(boolean b) {
        return b ? "true" : "false";
    }

tip2: 对于Byte而言值的范围在[-128&#xff0c;127] 

byte占一个字节空间&#xff0c;最高位是符号位&#xff0c;剩余7位能表示0-127&#xff0c;加上符号位的正负&#xff0c;就是-127至&#43;127&#xff0c;但负0没必要&#xff0c;为充分利用&#xff0c;就用负零表示-128&#xff08;即原码1000&#xff0c;0000&#xff09;。&#xff08;计算机转补码后存储&#xff09;

对于boolean类型&#xff1a; 提供静态的2中枚举值

public static Boolean valueOf(boolean b) {return (b ? TRUE : FALSE);}

ByteCache 缓存 Byte 、ShortCache 缓存 Short 、 LongCache 缓存 Long <三者源码类似,>

private static class LongCache {private LongCache(){}static final Long cache[] &#61; new Long[-(-128) &#43; 127 &#43; 1];static {for(int i &#61; 0; i

CharacterCache 缓存 Character

private static class CharacterCache {private CharacterCache(){}static final Character cache[] &#61; new Character[127 &#43; 1];static {for (int i &#61; 0; i

测试&#xff1a;

public static void main(String[] args) {Long a &#61; 3L;Integer b &#61; 4;Character c &#61; 5;Byte d &#61; 7;Short e &#61; 1;System.out.println(a &#61;&#61; Long.valueOf(a));System.out.println(b &#61;&#61; Integer.valueOf(b));System.out.println(c &#61;&#61; Character.valueOf(c));System.out.println(d &#61;&#61; Byte.valueOf(d));System.out.println(e &#61;&#61; Short.valueOf(e));}

运行的结果都为true&#xff01;

在断点单测时发现&#xff1a;都跳入了valueOf方法&#xff01;


转:https://my.oschina.net/u/3434392/blog/3009210



推荐阅读
author-avatar
xuzhaotong
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有