首页 / 缓存 / Java知识点系列:包装类型的缓存
Java知识点系列:包装类型的缓存
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java知识点系列:包装类型的缓存,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1369字,纯文字阅读大概需要2分钟。
内容图文
Java知识点目录
先看一个问题
Integer int1 = 100;
Integer int2 = 100;
Integer int3 = 1000;
Integer int4 = 1000;
System.out.println("int1 == int2 :" + (int1 == int2));
System.out.println("int3 == int4 :" + (int3 == int4));
运行结果
这个例子只为了说明引用地址,包装类型相等判断应该用equals
int1 == int2 :true
int3 == int4 :false
Integer
以Integer为例,先看IntegerCache的源码。
/**
* 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 {@code -XX:AutoBoxCacheMax=<size>} 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 = -128;
static final int high;
static final Integer cache[];
//.....
}
根据注释,int类型在自动装箱成包装类型的时候,会使用缓存。创建范围在 -128 到 127 的包装类型时,先在缓存里查找对应的缓存对象,找到的话直接返回使用,未找到时创建一个新对象,并将新对象存到缓存。
-XX:AutoBoxCacheMax=<size> 这个JVM参数可以设置缓存的最大值 high。最小值low总是 -128。
其他包装类型
与Integer类似,Byte,Short,Long,Character也有这种缓存机制,其中Byte,Short,Long的缓存范围固定为 -128~127,Character的缓存范围是 \u005Cu0000 到 \u005Cu007F
内容总结
以上是互联网集市为您收集整理的Java知识点系列:包装类型的缓存全部内容,希望文章能够帮你解决Java知识点系列:包装类型的缓存所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。