integer为null_比较两个Integer,为什么必须使用equals?不用==
equals和==的区别
- 对于基本类型,比如int、long,进行判等,只能使用==,比较的是直接值,因为基本类型的值 就是其数值。
- 对应用类型,比如Integer、Long和String,进行判等,需要使用equals进行内容判等。因为引用类型的直接值是指针,使用==的话,比较的是指针,也就是两个对象在内存中的地址,即比较它们是不是同一个对象,而不是比较对象的内容。
总结:比较值的内容,除了基本类型只能使用==外,其他类型都需要使用equals。
测试 Integer用例
1、使用==对两个值127的直接赋值的Integer对象判断
Integer a = 127;Integer b = 127;System.out.println(a == b);//true
为什么结果是true呢?
编译器会把Integer a = 127转换为Integer.valudOf(127)。查看源码得知:
这个转换在内部其实做了缓存,是的两个Integer指向同一个对象,所以 == 返回true.
public static Integer valueOf(int i) {if (i >= IntegerCache.low && i <= IntegerCache.high)return IntegerCache.cache[i + (-IntegerCache.low)];return new Integer(i);}
其中 IntegerCache.low = static final int low = -128;
IntegerCache.high = 127
2、使用 == 对两个值 为128的直接赋值的Integer对象判等
Integer c = 128;Integer d = 128;System.out.println(c==d); //false
为什么会返回false呢?
默认情况下会缓存[-128,127]的数值,而128处于这个区间之外。
那么怎样让Integer缓存【-128,1000】呢?
可以设置JVM参数:-XX:AutoBoxCacheMax=1000
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() {}}
3、使用==对一个127的直接赋值的Integer 和 另一个通过new Integer声明的值为127的对象判等
Integer e = 127;Integer f = new Integer(127);System.out.println(e == f);//false
结果为false,为什么呢?
new 出来的Integer始终是不走缓存的新对象。比较一个新对象和一个来自缓存的对象,结果肯定不一样,因此返回false;
4、使用 == 对两个通过new Integer声明的 值为127的对象判等
Integer g = new Integer(127);Integer h = new Integer(127);System.out.println(g == h);//false
结果为false,为什么呢?
new 出来的Integer始终是不走缓存的新对象。比较两个新对象,结果肯定不一样,因此返回false;
5、使用 == 对一个值为128的直接赋值的Integer对象和另一个职位128的int基本类型判等
Integer i = 128;int j = 128;System.out.println(i == j);//true
结果为true,为什么呢?
Integer i = 128;是先拆箱再比较,比较的肯定是数值而不是引用,因此返回true。
总结:
只需要记得比较Integer的值请使用equals,而不是==,对于基本类型的int的比较当然只能使用==
Integer的误用
比如一个请假申请的状态,有这么一个枚举定义了请假的状态和对于状态的描述
public enum ProcessEnum {POST(1,"已提交"),AGREE(2,"同意"),REJECT(3,"拒绝");private Integer status;private String desc;ProcessEnum( Integer status, String desc ) { this.status = status;this.desc = desc;}}
在业务代码中,使用了 == 对枚举和入参ProcessQuery中的status属性进行判等;
public void compare(Integer status){ProcessEnum processEnum = ProcessEnum.AGREE; System.out.println(processEnum.status == status);}
因为枚举和入参中的status都是包装 类型,所以通过==判等肯定是有问题;
作者:码农的一天
原文:http://suo.im/5Wd5zB
integer为null_比较两个Integer,为什么必须使用equals?不用==相关推荐
- integer为null_走进 JDK 之 Integer
文中相关源码: Integer.java 开发的越久,越能体会到基础知识的重要性.抽空捋一下 JDK 源码,权当查漏补缺.读完之后,你会发现 JDK 源码真的会给你很多惊喜. Integer 是基本类 ...
- java中两个Integer类型的值相比较的问题
转载自: https://www.cnblogs.com/xh0102/p/5280032.html 两个Integer类型整数进行比较时,一定要先用intValue()方法将其转换为int数之后再进 ...
- 比较两个Integer的值是否相等
Integer比较大小的问题 遇到比较两个Integer值是否相等的问题,直接用"=="判断的,结果却是false. 下面看下例子: public class Test {publ ...
- integer 最大值_JAVA源码之Integer
一.Integer结构 Integer继承Number类,实现Comparable接口 基本数据类型的包装类除了Character和Boolean没有继承Number外,其它的都继承了 Number ...
- java integer == int_通过实例了解Java Integer类和int的区别
代码实例如下 public static void main(String[] args) { Integer i = 10; Integer j = 10; System.out.println(i ...
- java integer的默认值_int和Integer的默认值
群里有一个小伙伴发了一段和下面差不多的代码啊,问了一个问题,代码如下: public static voidmain(String[] args) { doem01 doem01= newdoem01 ...
- 面试题:两个对象值相同(x.equals(y) == true),但却可有不同的hash code,这句话对不对?
两个对象值相同(x.equals(y) == true),但却可有不同的hash code,这句话对不对? 对.如果是String等类的话,必须有相同的hashcode: 但若是这个类重写了hashc ...
- 两个 Integer 数值之间不建议使用 “==” 进行比较
阿里巴巴开发手册里面有一条强制的规则,说的是在包装类对象之间的值比较的时候需要使用 equals 方法,在 -128 和 127 之间的数值比较可以使用 ==,如下图所示.具体的原因相信大家都知道,虽 ...
- springboot~Integer和int如何选择,Integer的意义何在
今天说一下自己在项目中遇到的问题,然后总结一下Integer引用类型和int值类型 关于默认值 Integer默认为null int默认为0 为什么把数据实体设计成Integer或者不是int 大叔认 ...
最新文章
- Object o与Object o=null的区别
- oracle和mysql登录方式_使用普通方式和连接池方式获取Oracle和Mysql链接
- 体验使用node.js创建vue+Element-UI项目
- 6700设置上网教程
- bp神经网络应用实例_预测方法合集——BP神经网络
- 攻防世界(Pwn)dice_game, 栈溢出覆盖srand种子
- VTK:可视化算法之SplatFace
- vuejs 过渡效果
- SAP ABAP实用技巧介绍系列之 关于View framework处理Before save event的讨论
- GitHub 2019年度报告,用户超4000万
- Docker的基本认识及使用
- 基于昇腾处理器的目标检测应用(ACL)
- 将Go的main包拆分为多个文件
- SplitContainer的使用
- python中fn是什么意思_Python fn
- java使用openoffice/libreoffice进行office转pdf
- 需要点智商才能看懂的恐怖故事,你能看懂多少个?
- 考研要求过英语四六级!这些大学有明确规定!
- windows凭据管理
- ARM Neon Intrinsics各函数介绍
热门文章
- 红帽将停止支持 CentOS 8;​2020 百度沸点揭晓年度知识热词;Qt 6.0 发布|极客头条...
- 首届全国信创大赛圆满收官,信创新势力载誉而归!
- 机器人也开始怕疼了?科学家开发无需人工干预即可自愈的机器人
- 被忽视的代码审查,往往可以事半功倍?
- 用 Python 轻松搞定 Excel 中的 20 个常用操作
- 为什么谈及硬件,必言软件?软硬件协同让开源世界“阴阳调和”
- 从 0 到 70%:Chrome 上位揭秘!
- 关于 Docker ,你必须了解的核心都在这里
- JavaScript 为何会成为最受欢迎的编程语言?
- 2025 年全球数据量高达175ZB,开发者如何挑战数据洪流?