在Java中==的一个坑
观察下面代码,输出结果是什么?
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中==的一个坑相关推荐
- Java中MessageFormat的坑
文章目录 Java中MessageFormat的坑 问题现象 问题排查 如何解决 Java中MessageFormat的坑 问题现象 某个业务功能需要通过SSH协议执行命令查询一些数据,而某次查询居然 ...
- 如何在Java中创建一个新的List
本文翻译自:How to make a new List in Java We create a Set as: 我们创建一个Set为: Set myset = new HashSet() How d ...
- 怎么在java中创建一个自定义的collector
文章目录 简介 Collector介绍 自定义Collector 总结 怎么在java中创建一个自定义的collector 简介 在之前的java collectors文章里面,我们讲到了stream ...
- 怎么在java中关闭一个thread
怎么在java中关闭一个thread 我们经常需要在java中用到thread,我们知道thread有一个start()方法可以开启一个线程.那么怎么关闭这个线程呢? 有人会说可以用Thread.st ...
- java中判断一个字符在字符串中出现的次数
源代码: //java中判断一个字符出现的次数//在下面字符串中查找有几个啊public static void testFindChar(){String str = "啊!我爱你中国!啊 ...
- c++中的new_怎么在java中创建一个自定义的collector
简介 在之前的java collectors文章里面,我们讲到了stream的collect方法可以调用Collectors里面的toList()或者toMap()方法,将结果转换为特定的集合类. 今 ...
- thread java 关闭_怎么在java中关闭一个thread
怎么在java中关闭一个thread 我们经常需要在java中用到thread,我们知道thread有一个start()方法可以开启一个线程.那么怎么关闭这个线程呢? 有人会说可以用Thread.st ...
- java如何实例化集合_如何在java中实例化一个Queue对象?
Queue是一个接口,这意味着你不能直接构造一个Queue . 最好的select是构造一个已经实现Queue接口的类,如下所示: AbstractQueue , ArrayBlockingQueue ...
- java 整型数组定义_在Java中定义一个具有10个元素的整型数组a的语句是:___
在Java中定义一个具有10个元素的整型数组a的语句是:___ 答: int [] arr = new int[10] 在借贷记账法下() 答:在账户结构上,"借"和"贷 ...
最新文章
- 互联网大厂技术面试内幕@霞落满天
- R语言将字符型(Character)变量转化为数值型(Numeric)
- 互联网话题: 陈冠希, 张柏芝, 台湾, ASP与雪灾
- 软件工程----9软件实现
- html头部尾部分离组件引入(JQ)
- 利用记录型信号量解决不会出现死锁的哲学家就餐问题
- 用Chntpw cd disk 或老毛桃清除WIN7和2008密码
- shell基础之exit,break,continue
- python创建员工_Python综合练习之创建员工信息表
- WinMTR-路由追踪软件
- 局域网文档服务器搭建,局域网服务器的搭建.pdf
- MYSQL安装+Mysql-front 注册码
- erp和php数据对接,ERP系统与PLC数据对接方法
- bitcoin简析一
- Python-mne库使用教程
- SRE学堂:OSS监控告警案例分析
- c语言读音,C语言的32个关键字(读音、用法、注释)转来的,给刚接触C的
- 网络推广文案标题的重要性
- 四平方和定理_简化循环
- 科学可视化与信息可视化