Java Integer类型比较
今天做了一道题目题目如下:
Integer a=10;
Integer b=10;
System.out.print(a==b);
Integer c=200;
Integer d=200;
System.out.print(c==d);
请说出输出:
答案为:true,false
是不是很奇怪?
翻源码去哈哈;
查看Integer.value(int i)方法
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)//判断如果大于low或者小于high
return IntegerCache.cache[i + (-IntegerCache.low)];//从这个cache中取出返回
return new Integer(i);//返回新实例
}
查看内部类
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");//获取jvm参数
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;//Debug 断言一下
}
private IntegerCache() {}
}
可以看到还是可以设置jvm的参数的java.lang.Integer.IntegerCache.high来设置这个最大或者最小缓存区,但是会导致这个Integer的cache数组的大小变大
在没设置参数的时候当Integer在-128到127之间的时候,会从cache数组中取出该对象,这样两者比较就是相同的啦。
上面的面试题也就可以解释啦
Java Integer类型比较的更多相关文章
- 转:JAVA里面的int类型 和Integer类型,有什么不一样
JAVA里面的int类型 和Integer类型,有什么不一样 原文链接:http://blog.csdn.net/wuxinliulei/article/details/11099565 java.l ...
- java对象中含有Integer类型字段转json字符串问题
问题:对于含有Integer类型字段的java对象,在通过下面这种方式转为json字符串时,Integer类型的字段如果为空的情况下,会默认转化为0,但是我想让它为空的时候直接转化为null,不要默认 ...
- JAVA里面的int类型 和Integer类型,有什么不一样
JAVA里面的int类型 和Integer类型,有什么不一样 原创 2013年09月04日 23:15:11 标签: java / 2120 编辑 删除 JAVA里面的int类型 和Integer类型 ...
- 面试官:如何在Integer类型的ArrayList中同时添加String、Character、Boolean等类型的数据? | Java反射高级应用
原文链接:原文来自公众号:C you again,欢迎关注! 1.问题描述 "如何在Integer类型的ArrayList中同时添加String.Character.Boolean等 ...
- Java中Integer类型的整数值的大小比较
如果比较两个数值相等的Integer类型的整数,我们可能会发现,用"=="比较(首先你必须明确"=="比较的是地址),有的时候返回true,而有的时候,返回fa ...
- java中两个Integer类型的值相比较的问题
今天在做一个算法时,由于为了和其他人保持接口的数据类型一致,就把之前的int换为Integer,前几天测了几组数据,和之前的结果一样,但是今天在测其它数据 的时候,突然出现了一个奇怪的bug,由于之前 ...
- java integer对象判断两个数字是否相等
java integer对象判断两个数字是否相等,不一定对 问题发生的背景:javaweb的项目,起先,因为在java中实体类中的int类型在对象初始化之后会给int类型的数据默认赋值为0,这样在很多 ...
- java Integer和int的拆箱与装箱
官网:http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html 1.赋值: a. 把int类型赋值给Integer类型:JVM会 ...
- Hibernate 分页时 Long 无法转化成Integer类型 异常
转自:http://loquat.iteye.com/blog/818547 报错:java.lang.Long cannot be cast to java.lang.Integer Long ...
随机推荐
- List源码学习之ArrayList
ArrayList 内部结构为一个可重复的对象数组(可存空对象). 内部有以下几个参数: ;/** * 用于空实例的共享空数组实例 */private static final Object[] EM ...
- 服务器大量的fin_wait1 状态长时间存在原因分析
有一台服务器,出现很多的fin_wait1状态的socket. 环境: [root@localhost ~]# uname -aLinux localhost.localdomain 2.6.32-3 ...
- 企业级分布式存储应用与实战-mogilefs实现
Mogilefs是什么 MogileFS是一个开源的分布式文件存储系统,由LiveJournal旗下的Danga Interactive公司开发.Danga团队开发了包括 Memcached.Mogi ...
- Linux指令--more,less
文章出处:http://www.cnblogs.com/peida/archive/2012/11/05/2754477.html,感谢原作者无私的分享. more命令,功能类似 cat ,cat命令 ...
- awk 实用技巧
awk 用法:awk ' pattern {action} ' 变量名 含义ARGC 命令行变元个数ARGV 命令行变元数组FILENAME 当前输入文件名FNR 当前文件中的记录号FS 输入域分隔符 ...
- mysql与nagios的结合使用
一. 对mysql建库建表,并测试数据 基本信息:库名:nh_nagios表名:nagios_alerts [root@nhserver2 ~]# mysql -u root -pEnter pass ...
- asp.net 发布程序到iis后无法连接到oralce数据库问题
在应用程序池里面,选中你的站点所使用的应用程序池->高级设置->启用32位应用程序->true
- MonogoDB 练习一
1.解析文件,仅处理 FIELDS 字典中作为键的字段,并返回清理后的值字典列表 需求: 1.根据 FIELDS 字典中的映射更改字典的键 2.删掉"rdf-schema#label&quo ...
- bootstrap---treeview使用方法
1.html部分: <div id="tree"></div> 2.css设置展开/收缩按钮图片: .tree_arrows_down:before{ co ...
- CS231n-lecture2-Image Classification pipeline 课堂笔记
---恢复内容开始--- 相关资源 Event Type Date Description Course Materials Lecture 2 Thursday April 6 Image ...