Java 基础巩固:装箱拆箱 你真的熟悉吗
2024-08-28 22:35:29
先考两道题:
Integer a1 = 300;
Integer a2 =300;
System.out.print(a1 == a2);
Integer b1 = 1;
Integer b2 = 1;
System.out.print(a1 == a2);
答案是:
false
true
Integer a1=1的时候,java编译器会默认编译成Integer a1=Integer.valueOf(1),valueOf方法里边会查看a1的值是否在-128到127,如果在这个范围,会从IntegerCache这个缓存中去取有没有值等于1的对象,如果有的话会返回缓存里的对象,没有的话会new一个。
马丹,那人说错了 integer 在-128 到127之间是静态初始化的 直接从缓存里面取得
JDK 1.7:
JDK 1.8:
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() {}
}
自动装箱的缓存??
Long 也是:
##Double,Float 不一样:
Double:
Float:
String??
http://blog.csdn.net/u011240877/article/details/47721135
最新文章
- [译] MongoDB Java异步驱动快速指南
- C# WebClient 使用http免费代理。
- JAVA中JDBC连接数据库
- reverse(), extend(), sort() methods of list
- 团队自动化环境搭建与管理--php博弈
- php上传常见文件类型对应的$_FILES[";file";][";type";](转)
- Java Instanceof
- android工具类常用方法
- hadoop性能测试命令
- 如何一步一步用DDD设计一个电商网站(十二)—— 提交并生成订单
- memcached优化方案实例
- 常用DOS命令之通俗易懂篇
- Log4j 2X 日志文件路径问题
- ext2文件系统学习(一)
- MySQL Network--Localhost与127.0.0.1的差异
- 数据分析——Matplotlib图形绘制
- CF815C Karen and Supermarket
- springcloud(十):服务网关zuul初级篇
- PHP中对象的传值方式
- c语言实现CRC校验和
热门文章
- 【千纸诗书】—— PHP/MySQL二手书网站后台开发之功能实现
- [Parcel] Running TypeScript with parcel-bundler
- WCF调试异常信息:ServiceHost 仅支持类服务类型
- webpack的配置文件entry与output
- js map、filter、forEach
- Cookie小案例-----记住浏览过的商品记录
- spring事务管理源码解析--加了@Transactional注解后Spring究竟为我们做了哪些事情?
- windows操作系统记事本保存操作时间、字符映射表的打开、步骤记录器使用
- ZooKeeper_基础知识学习
- C++程序设计(第4版)读书笔记_C++概览:基础知识