关注Java细节 -- 奇怪的Byte/Character/Short/Integer/Long类
来源:互联网 发布:qq游戏显示网络不给力 编辑:程序博客网 时间:2024/06/06 06:57
在这边文章中,我并不打算详细讲解整个Integer类,因为Integer作为int的包装类,它所提供的方法都是非常简单的,大部分方法通过名字就可以知道这个方法的功能,而其实现也都不是很复杂。
今天我们要讨论的是一个Integer类中比较奇怪的事实。
首选,让我们来看一个范例程序:
public class TestInteger {
public static void main(String[] args) {
Integer i1 = 123;
Integer i2 = 123;
System.out.println(i1 == i2);
Integer i3 = 128;
Integer i4 = 128;
System.out.println(i3 == i4);
}
}
----------------------------
让我们来看看运行的结果:
true
false
----------------------------
false是正常的,因为i3和i4虽然其中保存的数值都是128,但是他们是指向两个不同的对象,
因此在用 == 进行判断时,会得到false的结果。
为什么会有这样的结果呢?
经过测试,在-128 - 127之间的数,都会得到true的结果。
也就是说,在这个区间内,如果两个Interger被赋值为同样的数,那么这两个引用指向的是同一个对象。
这又是为什么呢???
在JDK的Integer类的定义中,我们找到了答案。
从JDK1.5以后,对于Integer对象的赋值操作会自动调用Integer类的valueOf(int i)函数,
其valueof函数的具体实现如下:
public static Integer valueOf(int i) {
final int offset = 128;
if (i >= -128 && i <= 127) { // must cache
return IntegerCache.cache[i + offset];
}
return new Integer(i);
}
这里我们发现,当输入的数值在-128到127这个区间内的时候,valueOf是直接返回了IntegerCache类中cache数组中的元素的。
我们再来看看IntegerCache类的定义:
private static class IntegerCache {
private IntegerCache(){}
static final Integer cache[] = new Integer[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Integer(i - 128);
}
}
到这里,一切都真相大白了。
原来JDK1.5以后,Java为了增强Integer类的性能,预先缓存了一部分Integer对象,也就是-128到127这个区间所对应的Integer对象。当用户请求这个区间内的对象时,直接将缓存的相应对象返回给用户,这也就照成了针对这个区间内所有用户应用指向同一个对象的结果。
-------------------------------
同样的问题在Long类、Short类、Byte类、Character类中也是有的。
其原理和Integer类是一样的。
下面列出Long类中对应的代码,供大家参考,其他的类的实现也都是类似的,这里就不一一列出了。
public static Long valueOf(long l) {
final int offset = 128;
if (l >= -128 && l <= 127) { // will cache
return LongCache.cache[(int)l + offset];
}
return new Long(l);
}
private static class LongCache {
private LongCache(){}
static final Long cache[] = new Long[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Long(i - 128);
}
}
- 关注Java细节 -- 奇怪的Byte/Character/Short/Integer/Long类
- Byte、 Boolean 、Integer、Long、Short、Character缓存
- Byte,Short,Integer,Long类
- Byte Short Integer Long Float Double类的使用
- 第八章--包装类(Integer对象的创建及Integer类提供的各种方法、Long、Short、Boolean、Byte、Character、Double、Float)
- j2se(一)_Number 类Integer、Long、Byte、Double、Float、Short
- java 中 byte,short,int,long的取值范围
- short、int、long与byte之间的转换工具类
- short、int、long与byte之间的转换工具类
- short、int、long与byte之间的转换工具类
- short、int、long与byte之间的转换工具类
- short、int、long与byte之间的转换工具类
- C的无符号数据类型int,short,byte 转为 Java对应的数据long,char,short
- C的无符号数据类型int,short,byte 转为 Java对应的数据long,char,short
- C的无符号数据类型int,short,byte 转为 Java对应的数据long,char,short
- Byte Short Int Long的长度
- byte short int long
- byte、short、int、long
- c++中抽象类、虚函数和纯虚函数的学习以及和Java的对比
- BLOG 开通啦
- VC 对话框上静态控件文字的自动滚动
- VS2005 'stdint.h': No such file or directory 的解决方法
- Web.xml文件的标签详解
- 关注Java细节 -- 奇怪的Byte/Character/Short/Integer/Long类
- ubuntu下AVR/MEGA16开发
- soap客户端请求包异常
- ATL问题集(精典整理2)
- oracle 存储过程访问其他用户表
- .net Assembly—治愈“DLL地狱”的良方?
- STL容器总结系列(1)
- Slickedit的设置篇
- 应用系统架构设计