奇怪的Java题:1000 == 1000为false,而100 == 100会为true?
来源:互联网 发布:windows路径长度 编辑:程序博客网 时间:2024/05/29 09:16
原文出处: 回忆和感动
这是我们今天要讨论的话题,因为我觉得它非常的有趣。
如果你运行如下代码:
1
2
3
4
Integer a =
1000
, b =
1000
;
System.out.println(a == b);
//1
Integer c =
100
, d =
100
;
System.out.println(c == d);
//2
你会得到以下运行结果:
1
2
false
true
我们知道,如果两个引用指向同一个对象,那么==就成立;反之,如果两个引用指向的不是同一个对象,那么==就不成立,即便两个引用的内容是一样的。因此,结果就会出现false。
这是非常有趣的地方。如果你查看Integer.java类,你会找到IntegerCache.java这个内部私有类,它为-128到127之间的所有整数对象提供缓存。
这个东西为那些数值比较小的整数提供内部缓存,当进行如此声明时:
1
Integer c =
100
;
它的内部就是这样的:
1
Integer i = Integer.valueOf(
100
);
如果我们观察valueOf()类函数,我们可以看到
1
2
3
4
5
public
static
Integer valueOf(
int
i) {
if
(i >= IntegerCache.low && i <= IntegerCache.high)
return
IntegerCache.cache[i + (-IntegerCache.low)];
return
new
Integer(i);
}
如果值在-128到127之间,它就会返回该缓存的实例。
因此。。。
1
Integer c =
100
, d =
100
;
两者指向同样的对象。
这就是为什么这段代码的结果为true了:
1
System.out.println(c == d);
现在你可能会问,为什么会为-128到127之间的所有整数设置缓存?
这是因为在这个范围内的小数值整数在日常生活中的使用频率要比其它的大得多,多次使用相同的底层对象这一特性可以通过该设置进行有效的内存优化。你可以使用reflection API任意使用这个功能。
运行下面的这段代码,你就会明白它的神奇所在了。
1
2
3
4
5
6
7
8
9
10
11
12
13
//获取Integer中的cache
Class<?> cache = Integer.class.getDeclaredClasses()[0]; //1
Field myCache = cache.getDeclaredField("cache"); //2
myCache.setAccessible(true);//3
//将Integer的cache放在新的Integer[]中
Integer[] newCache = (Integer[]) myCache.get(cache); //4
System.out.println("newCache.length:"+newCache.length);
for (int i = 0; i < newCache.length; i++) {
System.out.println("index"+i+":"+newCache[i]);
}
System.out.println("index132:"+newCache[132]);//output:4
System.out.println("index133:"+newCache[133]);//output:5
System.out.println(newCache[132] = newCache[133]);//output:5
newCache[132] = newCache[133]; //5
//在Integer缓存值得范围内创建对象并计算
int a = 2;
System.out.printf("%d",a);
System.out.println();
int b = a + a;
System.out.printf("%d",b);
System.out.println();
System.out.printf("%d + %d = %d", a, a, b); //
}
0 0
- 奇怪的Java题:1000 == 1000为false,而100 == 100会为true?
- java常量池概念 奇怪的Java题:为什么1000 == 1000返回为False,而100 == 100会返回为True?
- 奇怪的Java题:为什么1000 == 1000返回为False,而100 == 100会返回为True?
- 奇怪的Java题:为什么1000 == 1000返回为False,而100 == 100会返回为True?
- (转)奇怪的Java题:为什么1000 == 1000返回为False,而100 == 100会返回为True?
- 奇怪的Java题:为什么1000 == 1000返回为False,而100 == 100会返回为True?
- 奇怪的Java题:为什么1000 == 1000返回为False,而100 == 100会返回为True?
- 奇怪的Java题:为什么1000 == 1000返回为False,而100 == 100会返回为True
- 奇怪的Java题:为什么128 == 128返回为False,而127 == 127会返回为True?
- 奇怪的Java题:为什么128 == 128返回为False,而127 == 127会返回为True?
- 为什么Java中1000==1000为false而100==100为true?
- 为什么Java中1000==1000为false而100==100为true?
- 为什么Java中1000==1000为false而100==100为true
- 为什么Java中1000==1000为false而100==100为true?
- 为什么Java中1000==1000为false而100==100为true?
- 为什么Java中1000==1000为false而100==100为true?
- 为什么Java中1000==1000为false而100==100为true?
- 为什么Java中1000==1000为false而100==100为true?
- PHP调用Python失败的解决办法
- Oracle数据库教程
- Android状态栏微技巧
- 第十六周项目1-(6)验证算法选择排序之堆排序
- JDK1.7 sun.net.ftp.FtpClient
- 奇怪的Java题:1000 == 1000为false,而100 == 100会为true?
- iOS 加载Bundle文件
- iOS系统库头文件中NS_AVAILABLE相关
- 特征工程
- 为何Java中子类重写方法的访问权限不能低于父类中权限
- 基于nginx容器的动态流量管理方案
- 第十四周项目4—Floyd算法验证
- 几张GIF看ConstraintLayout的特新
- caffe中 solver.prototxt文件