首页 > 其他 > 详细

Integer的“==”

时间:2018-06-18 11:44:53      阅读:241      评论:0      收藏:0      [点我收藏+]
                Map<String,Integer> a=new HashMap(),b=new HashMap();
		a.put("a", 100);
		b.put("a", 100);
		a.put("c", 130);
		b.put("c", 130);
		System.out.println(a.get("a")==b.get("a"));//false
		System.out.println(a.get("c")==b.get("c"));//true
		System.out.println((Integer)130==130);//true
		System.out.println((Integer)100==100);

  在放入map的时候,自动将int 130转成了Integer,取出来的时候也是Integer,而Integer的==是判断引用等价性,

当如果整型字面量的值在-128到127之间,那么不会new新的Integer对象,而是直接引用常量池中的Integer对象

所以System.out.println(a.get("c")==b.get("c"));//true

而大于130,new 了新的Integer,System.out.println(a.get("a")==b.get("a"));//false

所以所有相同类型的包装类对象之间值得比较,全部使用equals方法。

有poJAVA源码参考的博客https://blog.csdn.net/so_geili/article/details/79720238

Integer的“==”

原文:https://www.cnblogs.com/blairwaldorf/p/9194724.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!