Integer 值判断相等

案例:

public class Test {public static void main(String[] args) {Integer a = 127;Integer b = 127;System.out.println("a == b :"+ (a == b));System.out.println("a.equals(b):"+a.equals(b));String x = "127";String y = "127";System.out.println("Integer.valueOf(x) == Integer.valueOf(y) :" + (Integer.valueOf(x) == Integer.valueOf(y)));System.out.println("Integer.valueOf(x).equals(Integer.valueOf(y)):"+Integer.valueOf(x).equals(Integer.valueOf(y)));System.out.println("====================================================================");Integer a1 = 128;Integer b1 = 128;System.out.println("a1 == b:"+(a1 == b1));System.out.println("a.equals(b):"+a.equals(b1));String x1 = "128";String y1 = "128";System.out.println("Integer.valueOf(x1) == Integer.valueOf(y1) :" + (Integer.valueOf(x1) == Integer.valueOf(y1)));System.out.println("Integer.valueOf(x1).equals(Integer.valueOf(y1)):"+Integer.valueOf(x1).equals(Integer.valueOf(y1)));}
}

日志打印:

a == b :true
a.equals(b):true
Integer.valueOf(x) == Integer.valueOf(y) :true
Integer.valueOf(x).equals(Integer.valueOf(y)):true
====================================================================
a1 == b:false
a.equals(b):false
Integer.valueOf(x1) == Integer.valueOf(y1) :false
Integer.valueOf(x1).equals(Integer.valueOf(y1)):true

通过案例发现,值为127不管是 Integer 还是 String 类型,== 和 equals 都能比较成功。128与类型无关,与比较的方法有关。

equals

通过查看源码发现 Integer 重写的 equals、hashCode 方法,所以使用 equals 方法比较大小母庸质疑为 true.

/*** Returns a hash code for this {@code Integer}.** @return  a hash code value for this object, equal to the*          primitive {@code int} value represented by this*          {@code Integer} object.*/@Overridepublic int hashCode() {return Integer.hashCode(value);}/*** Returns a hash code for a {@code int} value; compatible with* {@code Integer.hashCode()}.** @param value the value to hash* @since 1.8** @return a hash code value for a {@code int} value.*/public static int hashCode(int value) {return value;}/*** Compares this object to the specified object.  The result is* {@code true} if and only if the argument is not* {@code null} and is an {@code Integer} object that* contains the same {@code int} value as this object.** @param   obj   the object to compare with.* @return  {@code true} if the objects are the same;*          {@code false} otherwise.*/public boolean equals(Object obj) {if (obj instanceof Integer) {return value == ((Integer)obj).intValue();}return false;}

==

127和127比较返回true,128和128比较返回false,有点出乎意料,主要是因为我们使用了惯用思维“以为/觉得”他们相等,没有经过认证。Integer a = 127; 是自动装箱会调用Interger.valueOf(int)方法;

public static Integer valueOf(String s) throws NumberFormatException {return Integer.valueOf(parseInt(s, 10));}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 是-127,Integer.high是128,如果在这个区间内,他就会把变量i当做一个变量,放到内存中;但如果不在这个范围内,就会去new一个 Integer 对象,所以使用 “==” 比较127返回true,比较128返回 false。

Integer 值判断相等相关推荐

  1. Integer值判断是否相等

    在开发中遇到一个问题,定义了两个Integer变量,暂且定义为Integer a;  Integer b; 这一个值由前端赋值并传到后台,前台传的是a = 12345, 后台查询b = 12345, ...

  2. java integer 相等_Java Integer.equals()判断相等

    首页 > 基础教程 > 常用类 > 常用 Integer类 Java Integer.equals()判断相等 Java中判断Integer是否相等可以用equals()或者&quo ...

  3. java Integer等号判断

    <阿里Java开发手册>中有这样一项强制要求: "所有整形包装类对象之间值的比较,全部使用equals方法比较.说明:对于Integer var= ?在-128到127范围内的赋 ...

  4. java Integer值比较

    所有相同类型的包装类对象之间的值比较,应该使用equals方法比较. –来自阿里巴巴java开发手册. 先看个简单实例: public static void main(String[] args)t ...

  5. C语言学习之输入一个大于三的值判断是否为素数

    C语言学习之输入一个大于三的值判断是否为素数 方法一 #include <stdio.h> int main(){int number,x; //定义变量printf("请输入一 ...

  6. 392. 判断子序列 golang 关于布尔类型返回值判断的思考

    Me 1.取每一个字符串的字符进行比较. 2.相等就i++,j++,不相等就j++ 3.如果s空了,那么就是true.如果t字符串空了那么...有很多种情况.所以这个解法不好. 但是如果把返回条件设置 ...

  7. 信息学奥赛一本通 1042:奇偶ASCII值判断 | OpenJudge NOI 1.4 04

    [题目链接] ybt 1042:奇偶ASCII值判断 OpenJudge NOI 1.4 04:奇偶ASCII值判断 [题目考点] 1. if-else语句 2. 三目运算符?: 3. 判断奇数偶数 ...

  8. 信息学奥赛一本通(1042:奇偶ASCII值判断)

    1042:奇偶ASCII值判断 时间限制: 1000 ms         内存限制: 65536 KB 提交数: 57885     通过数: 34607 [题目描述] 任意输入一个字符,判断其AS ...

  9. java 雷达反射面积_请问:如何使用基本反射率雷达图中的dBZ值判断天气情况?比如多云、雷雨、晴等。谢谢!...

    请问:如何使用基本反射率雷达图中的dBZ值判断天气情况?比如多云.雷雨.晴等.谢谢!以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容,让我们赶快一起来 ...

最新文章

  1. scala class和object,trait的区别
  2. mac环境下安装xampp
  3. ue查找多个关键字_聊聊数据库GROUP BY关键字的那些事
  4. mysql数据库一些可能会用到的命令
  5. JavaScript中this详解
  6. Python——函数 8、可变长参数
  7. android.view.InflateException: Binary XML file line #34: Error inflating class
  8. poi导出excel设置对应格式
  9. 无法定位链接器!请检查 tools\link.ini 中的配置是否正确。 易语言5.9版本 静态编译
  10. 最简单DIY基于ESP8266的智能彩灯⑤(用C#调色板UI通过串口控制WS2812彩灯)
  11. 【游戏开发实战】重温红白机经典FC游戏,顺便教你快速搭建2D游戏关卡(Tilemap | 场景 | 地图)
  12. 我的知识网络结构图(2019)
  13. 抖音xlog算法分析
  14. ACM-ICPC 2018 焦作赛区网络预赛 L Poor God Water(BM算法)
  15. Python基础:文件和异常
  16. iOS-仿网易云音乐控制器
  17. ET、LT、EPOLLONESHOT
  18. 单精度浮点数和双精度浮点数_浮点数和双精度数之间的区别
  19. 点击文本框弹出热门标签
  20. centos7搭建owncloud私有云

热门文章

  1. mysql 数据库名称限制_mysql 数据库名称限制
  2. 普通调幅(AM)与抑制载波双边带调幅(DSB)matlab编程实现
  3. MessagePack Java Jackson Dataformat 在 Map 中不使用 String 为 Key 的序列化
  4. python爬虫:其他操作
  5. 【莫比乌斯反演】10.30破译密码
  6. jquery 简单分页插件jQuerypage
  7. 结对-人机对战象棋游戏-开发过程
  8. MySQL优化大总结
  9. [转]Asp.Net大型项目实践(11)-基于MVC Action粒度的权限管理【续】【源码在这里】(在线demo,全部源码)...
  10. make工具和Makefile基础语法(含有交叉编译、pthread_create()的处理)