今天做了一道题目题目如下:

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类型比较的更多相关文章

  1. 转:JAVA里面的int类型 和Integer类型,有什么不一样

    JAVA里面的int类型 和Integer类型,有什么不一样 原文链接:http://blog.csdn.net/wuxinliulei/article/details/11099565 java.l ...

  2. java对象中含有Integer类型字段转json字符串问题

    问题:对于含有Integer类型字段的java对象,在通过下面这种方式转为json字符串时,Integer类型的字段如果为空的情况下,会默认转化为0,但是我想让它为空的时候直接转化为null,不要默认 ...

  3. JAVA里面的int类型 和Integer类型,有什么不一样

    JAVA里面的int类型 和Integer类型,有什么不一样 原创 2013年09月04日 23:15:11 标签: java / 2120 编辑 删除 JAVA里面的int类型 和Integer类型 ...

  4. 面试官:如何在Integer类型的ArrayList中同时添加String、Character、Boolean等类型的数据? | Java反射高级应用

    原文链接:原文来自公众号:C you again,欢迎关注! 1.问题描述     "如何在Integer类型的ArrayList中同时添加String.Character.Boolean等 ...

  5. Java中Integer类型的整数值的大小比较

    如果比较两个数值相等的Integer类型的整数,我们可能会发现,用"=="比较(首先你必须明确"=="比较的是地址),有的时候返回true,而有的时候,返回fa ...

  6. java中两个Integer类型的值相比较的问题

    今天在做一个算法时,由于为了和其他人保持接口的数据类型一致,就把之前的int换为Integer,前几天测了几组数据,和之前的结果一样,但是今天在测其它数据 的时候,突然出现了一个奇怪的bug,由于之前 ...

  7. java integer对象判断两个数字是否相等

    java integer对象判断两个数字是否相等,不一定对 问题发生的背景:javaweb的项目,起先,因为在java中实体类中的int类型在对象初始化之后会给int类型的数据默认赋值为0,这样在很多 ...

  8. java Integer和int的拆箱与装箱

    官网:http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html 1.赋值: a. 把int类型赋值给Integer类型:JVM会 ...

  9. Hibernate 分页时 Long 无法转化成Integer类型 异常

    转自:http://loquat.iteye.com/blog/818547 报错:java.lang.Long cannot be cast to java.lang.Integer   Long ...

随机推荐

  1. nginx中location匹配顺序

    一.location语法 语法: Syntax: location [ = | ~ | ~* | ^~ ] uri { ... } location @name { ... } Default: - ...

  2. react项目中遇到的坑

    1,touchStart和touchEnd 如果touchstart和touchend改变的是同一个state,那么在首次加载渲染的时候组件会陷入死循环,原因是touchstart会直接触发,但此时s ...

  3. iOS学习——获取当前最顶层的ViewController

    在iOS开发过程中,我们经常性会需要获取当前页面的ViewController,然后利用ViewController进行一些操作,例如在最顶层的ViewController上展示一个UIAlertCo ...

  4. Git知识总览(四) git分支管理之rebase 以及 cherry-pick相关操作

    上篇博客聊了<Git知识总览(三) 分支的创建.删除.切换.合并以及冲突解决>,本篇博客我们主要来看一下 rebase 变基相关的操作.rebase 操作和 merge 操作最终都可以达到 ...

  5. 用 chrome 调试 node.js 代码

    1.全局安装 node-inspector  cnpm install -g node-inspector 2.启动node项目入口文件,如 node --inspect index.js 3.控制台 ...

  6. js中的监听事件总结

    javascript事件与功能说明大全:http://tools.jb51.net/table/javascript_event 1.滚动条监听事件 例1:监听滚动条距离页面顶端距离 <scri ...

  7. windows Apache服务器配置

    Apache 64位可以而32位不可以 安装Apache服务 注意: 如果没有自己设置Apache服务名,后面都可不跟-n "服务名",即采用默认的服务名称. 必须用管理员提示符打 ...

  8. execl列数据成等差递增递减

    如上图若想以10,20,30...这样递增: 1).首先需选中10,20所在的单元格,鼠标移至20所在的单元格右下角 2).此时会出现一个十字"十"符号,点击直向下拖动至某个地方, ...

  9. 浅尝一个排程引擎Optaplanner,前序。

    当码农有10多年了,由建筑行业软件,各种MIS,通用物流定制平台,CCTV客户端(是闭路电视,不是央视喔)啥都做过.最后小试一下创业,不过那都是闹着玩的,不到一年就回到码农的队列,重拾搬砖的行当.近些 ...

  10. 14_Python字符串操作方法总结

    字符串方法总结 #s = '**i love you\n\t' 测试s.strip()使用的字符串 s = 'i love you' #1.首字符大写,其余字符小写 print(s.capitaliz ...