包装类型的缓存机制

Java 基本数据类型的包装类型的大部分都用到了缓存机制来提升性能。

Byte,Short,Integer,Long 这 4 种包装类默认创建了数值 -128,127 的相应类型的缓存数据,Character 创建了数值在 0,127 范围的缓存数据,Boolean 直接返回 True or False

Integer 缓存源码:

复制代码
public static Integer valueOf(int i) {
    if (i >= IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
}
private static class IntegerCache {
    static final int low = -128;
    static final int high;
    static {
        // high value may be configured by property
        int h = 127;
    }
}

Character 缓存源码:

复制代码
public static Character valueOf(char c) {
    if (c <= 127) { // must cache
      return CharacterCache.cache[(int)c];
    }
    return new Character(c);
}

private static class CharacterCache {
    private CharacterCache(){}
    static final Character cache[] = new Character[127 + 1];
    static {
        for (int i = 0; i < cache.length; i++)
            cache[i] = new Character((char)i);
    }

}

Boolean 缓存源码:

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

如果超出对应范围仍然会去创建新的对象,缓存的范围区间的大小只是在性能和资源之间的权衡。

两种浮点数类型的包装类 Float,Double 并没有实现缓存机制。

复制代码
Integer i1 = 33;
Integer i2 = 33;
System.out.println(i1 == i2);// 输出 true

Float i11 = 333f;
Float i22 = 333f;
System.out.println(i11 == i22);// 输出 false

Double i3 = 1.2;
Double i4 = 1.2;
System.out.println(i3 == i4);// 输出 false

下面我们来看一个问题:下面的代码的输出结果是 true 还是 false 呢?

复制代码
Integer i1 = 40;
Integer i2 = new Integer(40);
System.out.println(i1==i2);// 输出false

Integer i1=40 这一行代码会发生装箱,也就是说这行代码等价于 Integer i1=Integer.valueOf(40) 。因此,i1 直接使用的是缓存中的对象。而Integer i2 = new Integer(40) 会直接创建新的对象。因此,答案是 false 。你答对了吗?

记住:所有整型包装类对象之间值的比较,全部使用 equals 方法比较

更多消息资讯,请访问昂焱数据。昂焱数据

相关推荐
飞翔的馒11 分钟前
浏览器缓存之【结构化数据库与缓存】: IndexedDB、Cache storage 和 Storage buckets
数据库·缓存
怪兽学LLM3 小时前
AI 应用中的高并发处理:从限流、异步到缓存与队列
人工智能·缓存
喜欢的名字被抢了5 小时前
长会话状态治理:当Redis失忆后,系统如何自己找回忆
数据库·redis·缓存
Alan_69118 小时前
商品详情优化三板斧-拆分-多级缓存-GC调参
后端·缓存
難釋懷20 小时前
Nginx-proxy缓存断点续传缓存 range
运维·nginx·缓存
杨运交21 小时前
[053][核心模块]Java枚举缓存与ORM集成实践
java·开发语言·缓存
liulilittle1 天前
LLM推理中的KV缓存与跨请求前缀复用:机制、条件与提示词
缓存·ai·llm
智码看视界1 天前
Day33-数据层 × 中间件AI化篇:Redis缓存经典问题-击穿、穿透、雪崩的终极解决方案
数据库·redis·缓存·中间件·穿透·雪崩·击穿
難釋懷1 天前
Nginx-proxy缓存清理
运维·nginx·缓存
咩咩啃树皮2 天前
第43篇:Vue3计算属性(computed)完全精讲——缓存机制、依赖计算、业务最优解
前端·vue.js·缓存