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

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. Spring 数据库连接(Connection)绑定线程(Thread)的实现

    最近在看spring事务的时候在想一个问题:spring中的很多bean都是单例的,是非状态的,而数据库连接是一种有状态的对象,所以spring一定在创建出connection之后在threadloc ...

  2. JS-输入金额校验

    function clearNoNum(obj){    obj.value = obj.value.replace(/[^\d.]/g,"");  //清除"数字&qu ...

  3. Django 初识

    Django  初识 一.前言 Django是一款网站架构,能够快速的搭建一个网站.openstack的界面显示使用的就是Django的框架.所以,学习openstack多少要了解一些Django的内 ...

  4. tone() 和 IRremote 冲突的解决办法

    tone()函数冲突 http://www.geek-workshop.com/thread-4037-1-1.html 可以自制函数newtone() void newtone(byte toneP ...

  5. android onSaveInstanceState应用实例

    //activity销毁之前调用,把状态值存储上 @Override protected void onSaveInstanceState(Bundle outState) { outState.pu ...

  6. Spring MVC (JDK8+Tomcat8)

    1 Spring MVC概述 Spring MVC是Spring为表现层提供的基于MVC设计理念的优秀的web框架,是目前最主流的MVC框架之一. Spring3.0后全面超越Struts2,成为最优 ...

  7. 【转】EI收录的中国期刊

    ISSN     期刊名  0567-7718 Acta Mechanica Sinica  1006-7191 Acta Metallurgica Sinica (English Letters)  ...

  8. spring之构造注入

    第一种:通过构造name和value属性(不常用) <!-- userAction --> <bean id="userAction" class="c ...

  9. 跟我一起读postgresql源码(十六)——Executor(查询执行模块之——control节点(下))

    5.ModifyTable节点 先看一个ModifyTable节点的例子: postgres=# explain update test_01 set id = 5 where name = 'xxx ...

  10. JSON入门看这一篇就够了

    什么是JSON JSON:JavaScript Object Notation [JavaScript 对象表示法] JSON 是存储和交换文本信息的语法.类似 XML. JSON采用完全独立于任何程 ...