Java数据类型之Cache模式

1、关于Java数据类型

基本数据类型

基本数据类型有8种,每种基本数据类型都有对应的引用类型。

类型 描述 长度 可表示数据 包装类型
boolean 布尔型 1 true、false Boolean
byte 字节型 1 2-7~27-1 Byte
char 字符型 2 2-15~215-1 Character
short 短整型 2 2-15~215-1 Short
int 整型 4 2-31~231-1 Integer
float 浮点型 4 2-31~231-1 Float
long 长整型 8 2-63~263-1 Long
double 双精度浮点型 8 2-63~263-1 Double

为什么要有包装类型?

因为Java是面向对象语言,很多地方用到的是对象,而不是基本数据类型。比如集合类中,我们是无法定义集合的泛型是基本数据类型的。而包装类,顾名思义,就是将基本数据类型包装起来,使其具备了对象的性质,也为其添加了很多操作方法。

自动装箱与拆箱

自动装箱: 就是将基本数据类型自动转换成对应的包装类。

自动拆箱:就是将包装类自动转换成对应的基本数据类型。

为什么要有自动装拆箱呢?因为很多地方都是需要其进行转换的,而重复操作又会显得很多余,所以为其提供了自动适配功能。

那么哪些地方能用到呢?举两个最常用的例子。

  1. 类型转换
Integer i = 10; // 自动装箱
int a = i; // 自动拆箱

2)存入集合

List<Integer> list = new ArrayList<Integer>();
int a = 1;
list.add(a); // 自动装箱

2、Cache

顾名思义,Cache就是缓存的意思。那数据类型里面哪些地方用到Cache呢?它具备什么作用?

首先抛出一道题,请大家参考。

Integer a = Integer.valueOf(20);
Integer b = Integer.valueOf(20);
System.out.println(a == b);

结果相等吗?是相等的。输出true。

Integer a = Integer.valueOf(128);
Integer b = Integer.valueOf(128);
System.out.println(a == b);

这里结果不相等的,为啥?128!=128?

首先来看valueOf这个方法:

  • Integer valueOf(String s, int radix)

将字符串以规定进制转换成Integer,radix表示进制数。

  • Integer valueOf(String s)

将字符串转换为10进制的Integer。

  • Integer valueOf(int i)

将基本数据类型转换为包装类。

为什么会造成两个相同数字比较出来不相同呢?来,我们上源码。

前两个方法最终调用的方法:

    public static Integer valueOf(int i) {
        // 如果值在IntegerCache的低位和高位之间就从IntegerCache里取
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        // 否则new一个对象
        return new Integer(i);
    }

IntegerCache是Integer中的一个静态内部类

private static class IntegerCache {
        // 低位固定为-128
        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);
                    // 将配置值与127比较取最大值
                    i = Math.max(i, 127);
                    // 确定高位值,防止数组长度超过整型最大值
                    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)
            // 如果高位值大于等于127,则抛出异常
            assert IntegerCache.high >= 127;
        }

        private IntegerCache() {}
    }

因为128超过了整型缓存区域,所以每次都会new一个对象,所以导致比较出来不相等。

Short、Long、Character等内部都有Cache区域,建议大家多去挖掘挖掘。

原文地址:https://www.cnblogs.com/yl-space/p/13528816.html