观察下面代码,输出结果是什么?

    public static void main(String[] args) {Integer p = 10000;Integer q = 10000;System.out.println(p == q);System.out.println(p.equals(q));}

运行一次,答案与预想的完全不一样。在比较数字的时候,一定要用euqals,不能用==c

查看Integer的jdk源码,发现如下片段:

/*** Cache to support the object identity semantics of autoboxing for values between* -128 and 127 (inclusive) as required by JLS.** The cache is initialized on first usage.  The size of the cache* may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.* During VM initialization, java.lang.Integer.IntegerCache.high property* may be set and saved in the private system properties in the* sun.misc.VM class.*/private static class IntegerCache {static final int low = -128;static final int high;static final Integer cache[];static {// high value may be configured by propertyint h = 127;String integerCacheHighPropValue =sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");if (integerCacheHighPropValue != null) {try {int i = parseInt(integerCacheHighPropValue);i = Math.max(i, 127);// Maximum array size is Integer.MAX_VALUEh = 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;}private IntegerCache() {}}

这儿的IntegerCache有一个静态的Integer数组,在类加载时就将-128 到 127 的Integer对象创建了,并保存在cache数组中,一旦程序调用valueOf 方法,如果i的值是在-128 到 127 之间就直接在cache缓存数组中去取Integer对象。

如果是-128 到 127 的值,你不管创建多少个这个范围内的Integer用ValueOf出来的都是同一个对象。

转载于:https://www.cnblogs.com/xcr1234/p/6384705.html

在Java中==的一个坑相关推荐

  1. Java中MessageFormat的坑

    文章目录 Java中MessageFormat的坑 问题现象 问题排查 如何解决 Java中MessageFormat的坑 问题现象 某个业务功能需要通过SSH协议执行命令查询一些数据,而某次查询居然 ...

  2. 如何在Java中创建一个新的List

    本文翻译自:How to make a new List in Java We create a Set as: 我们创建一个Set为: Set myset = new HashSet() How d ...

  3. 怎么在java中创建一个自定义的collector

    文章目录 简介 Collector介绍 自定义Collector 总结 怎么在java中创建一个自定义的collector 简介 在之前的java collectors文章里面,我们讲到了stream ...

  4. 怎么在java中关闭一个thread

    怎么在java中关闭一个thread 我们经常需要在java中用到thread,我们知道thread有一个start()方法可以开启一个线程.那么怎么关闭这个线程呢? 有人会说可以用Thread.st ...

  5. java中判断一个字符在字符串中出现的次数

    源代码: //java中判断一个字符出现的次数//在下面字符串中查找有几个啊public static void testFindChar(){String str = "啊!我爱你中国!啊 ...

  6. c++中的new_怎么在java中创建一个自定义的collector

    简介 在之前的java collectors文章里面,我们讲到了stream的collect方法可以调用Collectors里面的toList()或者toMap()方法,将结果转换为特定的集合类. 今 ...

  7. thread java 关闭_怎么在java中关闭一个thread

    怎么在java中关闭一个thread 我们经常需要在java中用到thread,我们知道thread有一个start()方法可以开启一个线程.那么怎么关闭这个线程呢? 有人会说可以用Thread.st ...

  8. java如何实例化集合_如何在java中实例化一个Queue对象?

    Queue是一个接口,这意味着你不能直接构造一个Queue . 最好的select是构造一个已经实现Queue接口的类,如下所示: AbstractQueue , ArrayBlockingQueue ...

  9. java 整型数组定义_在Java中定义一个具有10个元素的整型数组a的语句是:___

    在Java中定义一个具有10个元素的整型数组a的语句是:___ 答: int [] arr = new int[10] 在借贷记账法下() 答:在账户结构上,"借"和"贷 ...

最新文章

  1. 互联网大厂技术面试内幕@霞落满天
  2. R语言将字符型(Character)变量转化为数值型(Numeric)
  3. 互联网话题: 陈冠希, 张柏芝, 台湾, ASP与雪灾
  4. 软件工程----9软件实现
  5. html头部尾部分离组件引入(JQ)
  6. 利用记录型信号量解决不会出现死锁的哲学家就餐问题
  7. 用Chntpw cd disk 或老毛桃清除WIN7和2008密码
  8. shell基础之exit,break,continue
  9. python创建员工_Python综合练习之创建员工信息表
  10. WinMTR-路由追踪软件
  11. 局域网文档服务器搭建,局域网服务器的搭建.pdf
  12. MYSQL安装+Mysql-front 注册码
  13. erp和php数据对接,ERP系统与PLC数据对接方法
  14. bitcoin简析一
  15. Python-mne库使用教程
  16. SRE学堂:OSS监控告警案例分析
  17. c语言读音,C语言的32个关键字(读音、用法、注释)转来的,给刚接触C的
  18. 网络推广文案标题的重要性
  19. 四平方和定理_简化循环
  20. 科学可视化与信息可视化

热门文章

  1. java同类型同字段名称对象赋值
  2. Spring @Value转义冒号(:)的默认值
  3. 富文本转换字符串 php,php 如何将一个富文本字符串生成word文档?
  4. c 命令行简易计算器
  5. EXSi5.5安装篇
  6. CSS——NO.9(颜色值和长度值)
  7. jquery获取父级元素和子级元素
  8. MySQL 5.7原生JSON格式支持
  9. 软件设计心情笔记(一)目的与手段都很重要
  10. HTML5网站大观:分享8个精美的 HTML5 网站案例