我是靠谱客的博主 留胡子乌冬面,最近开发中收集的这篇文章主要介绍java返回两个Integer值_判断两个Integer值是否相等,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

代码如下:

/**

* @comment:Integer 类型数值比较

* @author:傅里叶级数

* @date:2018年8月

* @param args

*/

public static void main(String[] args) {

Integer a =127;

Integer b = 128;

Integer c =127;

Integer d =128;

Integer e=-128;

Integer f = -128;

Integer g = -129;

Integer h = -129;

//打印int类型的hash值:

System.out.println(System.identityHashCode(a));//644117698

System.out.println(System.identityHashCode(c));//644117698

System.out.println(System.identityHashCode(h));//1872034366

System.out.println(System.identityHashCode(g));//1581781576

System.out.println(a==c);//true

System.out.println(b==d);//false

System.out.println(f==e);//true

System.out.println(g==h);//false

}

源码解释:

/**

* Returns an {@code Integer} instance representing the specified

* {@code int} value. If a new {@code Integer} instance is not

* required, this method should generally be used in preference to

* the constructor {@link #Integer(int)}, as this method is likely

* to yield significantly better space and time performance by

* caching frequently requested values.

*

* This method will always cache values in the range -128 to 127,

* inclusive, and may cache other values outside of this range.

*

* @param i an {@code int} value.

* @return an {@code Integer} instance representing {@code i}.

* @since 1.5

*/

public static Integer valueOf(int i) {

if (i >= IntegerCache.low && i <= IntegerCache.high)

return IntegerCache.cache[i + (-IntegerCache.low)];

return new Integer(i);

}

综上,如果两个Integer的值在[-128,127]之间,==比较返回值为true;否则可能(may)new一个对象,hash地址不同,==返回false。

转载至链接:https://my.oschina.net/FourierSeriesNzh/blog/1928079

最后

以上就是留胡子乌冬面为你收集整理的java返回两个Integer值_判断两个Integer值是否相等的全部内容,希望文章能够帮你解决java返回两个Integer值_判断两个Integer值是否相等所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(94)

评论列表共有 0 条评论

立即
投稿
返回
顶部