java包装类的缓存机制(转)
出处: java包装类的缓存机制
java 包装类的缓存机制,是在Java 5中引入的一个有助于节省内存、提高性能的功能,只有在自动装箱时有效
Integer包装类
举个栗子:
Integer a = 127;
Integer b = 127;
System.out.println(a == b); // true
这段代码输出的结果为true
使用自动装箱将基本类型转为封装类对象这个过程其实底层实现是调用封装类的valueOf方法:
Integer a =127; 相当于 Integer a = Integer.valueOf(127);
看一下Integer的valueOf方法:
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
如果入参 i 大于等于IntegerCache.low或者小于等于IntegerCache.high),就从IntegerCache中获取对象
看一下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(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() {}
}
默认范围为:-128到127之间,范围的最大值可以通过java.lang.Integer.IntegerCache.high设置,通过for循环将范围内的数据实例化为Integer对象放到cache数组里
在测试一下:
Integer a = 128;
Integer b = 128;
System.out.println(a == b); // false
输出结果为false,所以如果没有指定cache最大值时,在-128到127之间使用自动装箱时,会使用缓存
Byte包装类
再举个栗子:
public static void main(String[] args) {
Byte a = 127;
Byte b = 127;
System.out.println(a == b); //true
}
由于Byte范围在-128到127之间,所以Byte的valueOf都是从ByteCache缓存中获取的
public static Byte valueOf(byte b) {
final int offset = 128;
return ByteCache.cache[(int)b + offset];
}
ByteCache类:
private static class ByteCache {
private ByteCache(){}
static final Byte cache[] = new Byte[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Byte((byte)(i - 128));
}
}
与IntegerCache相比,ByteCache的最大值是不能修改的就是127
Short包装类
public static Short valueOf(short s) {
final int offset = 128;
int sAsInt = s;
if (sAsInt >= -128 && sAsInt <= 127) { // must cache
return ShortCache.cache[sAsInt + offset];
}
return new Short(s);
}
ShortCache类:
private static class ShortCache {
private ShortCache(){}
static final Short cache[] = new Short[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Short((short)(i - 128));
}
}
ShortCache的最大值也不可以修改,范围只能在-128 ~ 127之间
Long包装类的valueOf方法和LongCache类与Short包装类的实现一致,范围也是只能在-128 ~ 127之间
Character包装类
valueOf方法:
public static Character valueOf(char c) {
if (c <= 127) { // must cache
return CharacterCache.cache[(int)c];
}
return new Character(c);
}
CharacterCache类:
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);
}
}
Character的缓存范围在0 ~ 127之间
Boolean包装类
valueOf方法:
public static Boolean valueOf(boolean b) {
return (b ? TRUE : FALSE);
}
TRUE跟FALSE都是static final修饰的静态变量
public static final Boolean TRUE = new Boolean(true);
public static final Boolean FALSE = new Boolean(false);
Float包装类 & Double包装类
valueOf方法:
public static Float valueOf(float f) {
return new Float(f);
}
public static Double valueOf(double d) {
return new Double(d);
}
Float和Double没有使用缓存,直接new的对象
总结:
java的包装类中:Byte,Short,Integer,Long,Character使用static代码块进行初始化缓存,其中Integer的最大值可以通过java.lang.Integer.IntegerCache.high设置;Boolean使用static final实例化的对象;Float和Double直接new的对象没有使用缓存
java包装类的缓存机制(转)的更多相关文章
- JAVA包装类的缓存范围
JAVA包装类的缓存范围 前两天面试遇到两个关于JAVA源码的问题,记录下来提醒自己. 1.写出下面的输出结果 System.out.println(Integer.valueOf("100 ...
- java——包装类数据缓存 ==号详解
Java对部分经常使用的数据采用缓存技术,即第一次使用该数据则创建该数据对象并对其进行缓存, 当再次使用等值对象时直接从缓存中获取,从而提高了程序执行性能.(只对常用数据进行缓存) Java中只是对部 ...
- JAVA记录-redis缓存机制介绍(一)
1.redis介绍 Redis 简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis 与其他 key - value 缓存产品有以下三个特点: Re ...
- JAVA记录-redis缓存机制介绍(四)
Redis 数据备份与恢复 Redis SAVE 命令用于创建当前数据库的备份. 语法 redis Save 命令基本语法如下: redis 127.0.0.1:6379> SAVE 实例 re ...
- JAVA记录-redis缓存机制介绍(三)
Redis 事务 Redis 事务可以一次执行多个命令, 并且带有以下两个重要的保证: 事务是一个单独的隔离操作:事务中的所有命令都会序列化.按顺序地执行.事务在执行的过程中,不会被其他客户端发送来的 ...
- JAVA记录-redis缓存机制介绍(二)
Redis 集合(Set) Redis的Set是string类型的无序集合.集合成员是唯一的,这就意味着集合中不能出现重复的数据. Redis 中 集合是通过哈希表实现的,所以添加,删除,查找的复杂度 ...
- Java的自动拆装箱与Integer的缓存机制
转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10832303.html 一:基本类型与包装类型 我们知道,Java有8大基本数据类型,4整2浮1符1 ...
- java : 包装类 缓冲机制的使用(转载)
摘要: 八种基本数据类型和其包装类中 Integer valueOf(int i).Byte valueOf(byte b).Short valueOf(short s).Long valueOf(l ...
- Java包装类缓存
1.基本概念 在jdk1.5及之后的版本中,Java在5大包装类中(Byte,Charactor,Short,Integer,Long)增加了相应的私有静态成员内部类为相应包装类对象提供缓存机制,In ...
随机推荐
- git 新建仓库第一次提交
1 . git init //初始化仓库,在初始化的目录中会出现.git的文件夹 2. git add .(文件name) //添加文件到本地仓 3. git commit -m "fi ...
- RecyclerView下拉刷新和上拉加载更多实现
RecyclerView下拉刷新和上拉加载更多实现 转 https://www.jianshu.com/p/4ea7c2d95ecf 在Android开发中,RecyclerView算是使用频率非 ...
- C之数据类型
java的数据类型 byte 1个字节 boolean 1个字节 short 2个字节 char 2个字节 int 4个字节 float 4个字节 long 8个字节 double 8个字节 c语言的 ...
- java time
package cn.itcast_04; import java.text.SimpleDateFormat; import java.util.Date; public ...
- C++ STL 排列 next_permutation prev_permutation
#include <iostream>#include <algorithm>#include <vector> using namespace std; int ...
- 获取当前运行的exe路径
void GetAppPath(CString& path) { TCHAR str[] = {}; GetModuleFileName(NULL,str,); wchar_t *pszPos ...
- Anaconda3详细安装使用教程及问题总结
1.Anaconda是什么? 简单来说,Anaconda是Python的包管理器和环境管理器. 先来解决一个初学者都会问的问题:我已经安装了Python,那么为什么还需要Anaconda呢?原因有以下 ...
- Goland 激活码
实测有效,分享下 Goland
- jprofile 远程监控linux上的jvm
环境 客户端:win7+jprofiler_windows-x64_10_0_4.exe linux服务器:tomcat7+jdk1.7+jprofiler_linux_10_0_4.sh 一.客户端 ...
- Flutter 的一些小技巧
1. 获取状态栏高度 import 'dart:ui'; MediaQueryData.fromWindow(window).padding.top 2. 设置AppBar的高度 Scaffold( ...