Java Long 对象的相等比较_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Java Long 对象的相等比较

Java Long 对象的相等比较

 2015/4/23 7:04:07  lihongtai  程序员俱乐部  我要评论(0)
  • 摘要:LongoriginalLong=(long)-128;LongnewLong=(long)-128;System.out.println("ObjectCompare:"+(originalLong==newLong));System.out.println("LongValueCompare:"+(originalLong.longValue()==newLong.longValue()));System.out.println("EqualsCompare:"+originalLong
  • 标签:Java
class="java" name="code">
Long originalLong = (long) -128;
		Long newLong = (long) -128;
		System.out.println("Object Compare:" + (originalLong == newLong));
		System.out.println("LongValue Compare:" + (originalLong.longValue() == newLong.longValue()));
		System.out.println("Equals Compare:" + originalLong.equals(newLong));


结果为:
Object Compare:true
LongValue Compare:true
Equals Compare:true

Long originalLong = (long) 127;
		Long newLong = (long) 127;
		System.out.println("Object Compare:" + (originalLong == newLong));
		System.out.println("LongValue Compare:" + (originalLong.longValue() == newLong.longValue()));
		System.out.println("Equals Compare:" + originalLong.equals(newLong));

结果为:
Object Compare:true
LongValue Compare:true
Equals Compare:true

Long originalLong = (long) -129;
		Long newLong = (long) -129;
		System.out.println("Object Compare:" + (originalLong == newLong));
		System.out.println("LongValue Compare:" + (originalLong.longValue() == newLong.longValue()));
		System.out.println("Equals Compare:" + originalLong.equals(newLong));


结果为:
Object Compare:false
LongValue Compare:true
Equals Compare:true

Long originalLong = (long) 128;
		Long newLong = (long) 128;
		System.out.println("Object Compare:" + (originalLong == newLong));
		System.out.println("LongValue Compare:" + (originalLong.longValue() == newLong.longValue()));
		System.out.println("Equals Compare:" + originalLong.equals(newLong));


结果为:
Object Compare:false
LongValue Compare:true
Equals Compare:true

总结:当Long对象的值在long类型的区间内是,比较对象的值是否相等可以两个对象直接用“==”比较,否则需要利用其他两种方式比较,才能得到正确的结果。
结论:比较两个Long对象昂的值是否相等的时候,直接使用其他两种方式比较,而不要使用“==”caozuofu.html" target="_blank">操作符。
上一篇: 项目开发中提升java性能方法详解 下一篇: 没有下一篇了!
发表评论
用户名: 匿名