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

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. IOS 设置文件是否使用ARC

    全项目设置:Building Settings修改Objective-C Automatic Reference Counting设置是否使用 单文件设置:Build Phases-Compile S ...

  2. linux_Nginx日志

    错误信息日志配置: 日志文件默认:/application/nginx/logs/erroe.log error_log logs/error.log error; # 不写默认就有,默认error, ...

  3. Python 字符串大小写操作

    #coding=utf-8 #python中字符串的操作 # 字符串的大小写 s='hello_wOrld_oF_you' upper_str = s.upper() print('全部大写: ',u ...

  4. Android开发模板代码(一)——简单打开图库选择照片

    首先,先贴上样本代码 //检查权限 public void checkPermission() { if (ContextCompat.checkSelfPermission(this, Manife ...

  5. 检查型异常(Checked Exception)与非检查型异常(Unchecked Exception)

    这两个概念看了忘,碰着了又看,老是傻傻的分不清楚,今天把心得结合从网上搜的资料简单整理一下,希望帮自己明确区分开这两个概念,并牢牢的记住 1.检查型异常(Checked Exception) 个人理解 ...

  6. jdk源码->集合->ConcurrentHashMap

    类的属性 public class ConcurrentHashMap<K,V> extends AbstractMap<K,V> implements ConcurrentM ...

  7. jQuery事件 (jQuery实现图片轮播)

    jQuery事件按执行时间,主要分为两种,第一种是在网页加载完执行,第二种绑定在元素中,由访问者某些行为触发. $(document).ready(function(){ //事件 }); $(&qu ...

  8. 【转】Matlab作图语句小结

    之前用Matlab作图,从网上找了些别人的例子,然后慢慢调参数.其实对很多命令,特别是对句柄不是很了解,今天简单总结了一下.下面用几个例子来说明:     ]);  首先,gcf是当前figure对象 ...

  9. C++ explicit关键字详解(转载)

    转载:https://www.cnblogs.com/ymy124/p/3632634.html 首先, C++中的explicit关键字只能用于修饰只有一个参数的类构造函数, 它的作用是表明该构造函 ...

  10. Docker+Jenkins持续集成环境(5): android构建与apk发布

    项目组除了常规的java项目,还有不少android项目,如何使用jenkins来实现自动构建呢?本文会介绍安卓项目通过jenkins构建的方法,并设计开发一个类似蒲公英的app托管平台. andro ...