先来看一段代码:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public class Main{
public static void main(String[] args){
Integer num1 = 100;
Integer num2 = 100;
Integer num3 = 200;
Integer num4 = 200;
'''//输出结果'''
System.out.println(num1==num2);
System.out.println(num3==num4);
}
}
|
猜猜结果是什么?
很多人都会认为结果全为true,但结果去不是这样的
true
false
为什么是这样的结果?如果用内存来解释结果的话,num1和num2指向的是同一个对象,而num3和num4则指向的确是不同的对象。接下来就告诉你为什么,看一看Integer类型的valueof方法的源码:
?
|
1
2
3
4
5
6
|
public static Integer valueOf(int i) {
assert IntegerCache.high >= 127;
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + 128];
return new Integer(i);
}
|
其中IntegerCache的实现:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
'''// IntegerCache,一个内部类,注意它的属性都是定义为static final'''
private static class IntegerCache {
static final int high; '''//缓存上界,暂为null'''
static final Integer cache[]; '''//缓存的整型数组'''
'''// 块,为什么定义为块'''
static {
final int low = -128; '''// 缓存下界,不可变了。只有上界可以改变'''
'''// high value may be configured by property'''
'''// h值,可以通过设置jdk的AutoBoxCacheMax参数调整(以下有解释),自动缓存区间设置为[-128,N]。注意区间的下界是固定'''
int h = 127;
if (integerCacheHighPropValue != null) {
'''// Use Long.decode here to avoid invoking methods that'''
'''// require Integer's autoboxing cache to be initialized'''
// 通过解码integerCacheHighPropValue,而得到一个候选的上界值'''
int i = Long.decode(integerCacheHighPropValue).intValue();
'''// 取较大的作为上界,但又不能大于Integer的边界MAX_VALUE'''
i = Math.max(i, 127);
'''// Maximum array size is Integer.MAX_VALUE'''
h = Math.min(i, Integer.MAX_VALUE - -low);
}
high = h; '''//上界确定'''
'''// 就可以创建缓存块,注意缓存数组大小'''
cache = new Integer[(high - low) + 1]; //
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++); '''// -128到high值逐一分配到缓存数组'''
}
private IntegerCache() {}
}
|
通过这两段代码可以看出,在通过valueof方法创建Integer类型对象时,取值范围为[-128,127],数值在这个区间里,指针指向IntegerCache.cache中已经存在的对象引用,当数值超出这个范围,就会创建一个新的对象。
有一点需要注意的是,并不是所有的类型都是这个范围,看Double类型:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
|
public class Main{
public static void main(String[] args){
Double i1 = 100.0;
Double i2 = 100.0;
Double i3 = 200.0;
Double i4 = 200.0;
System.out.println(i1==i2);
System.out.println(i3==i4);
}
}
|
最终的输出结果:
false
false
具体为什么回事这样的结果,大伙可以去看看源代码中Double valueof方法的实现,其和Integer valueof方法不同,是因为在某个范围内的整型数值的个数是有限的,而浮点数却不是。
注意,Integer、Short、Byte、Character、Long这几个类的valueOf方法的实现是类似的。
Double、Float的valueOf方法的实现是类似的。
拉下了一个,Boolean类型的结果有两个True or False。直接看源代码:
?
|
1
2
3
|
public static Boolean valueOf(boolean b) {
return (b ? TRUE : FALSE);
}
|
而其中的TRUE和FALSE是这样定义的:
?
|
1
2
3
4
5
6
7
|
public static final Boolean TRUE = new Boolean(true);
'''/** '''
'''* The <code>Boolean</code> object corresponding to the primitive '''
'''* value <code>false</code>. '''
'''*/'''
public static final Boolean FALSE = new Boolean(false);
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
相关文章
猜你喜欢
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 35
-
2025-05-25 77
-
2025-06-04 90
-
2025-05-27 78
-
2025-05-25 40
热门评论

